0

I have 3 tables. In table_1 and table_2 I have field called plevel. Third table is to join them and is. cars_user and have 2 columns - table1_plevel and table2_plevel The query that I want to select is

$q = mysqli_query($con, "SELECT * FROM `cars` AS c
      LEFT JOIN `cars_user` AS c2u ON c.cars_plevel = c2u.car_plevel
      LEFT JOIN `users` AS u ON c2u.user_plevel = u.users_plevel");   

When I run it I get that the column is Unknown but I know that this column is there.

 #1054 - Unknown column 'c.cars_plevel' in 'on clause' 

I have put references also in cars_user to cars.plevel and users.plevel

ALTER TABLE `cars_user` ADD FOREIGN KEY ( `cars_plevel` ) 
     REFERENCES `app`.`cars` (`plevel`) ON DELETE RESTRICT ON UPDATE RESTRICT;
ALTER TABLE `cars_user` ADD FOREIGN KEY ( `user_plevel` ) 
     REFERENCES `app`.`users` (`plevel`) ON DELETE RESTRICT ON UPDATE RESTRICT;

What is the problem here?

Erik BRB
  • 207
  • 6
  • 19
Jason Paddle
  • 1,168
  • 1
  • 17
  • 36

1 Answers1

0

I like error said you don't have c.cars_plevel you have c.plevel and you don't have u.users_plevel you have u.plevel or in other words:

$q = mysqli_query($con, "SELECT * FROM `cars` AS c
  LEFT JOIN `cars_user` AS c2u ON c.plevel = c2u.car_plevel
  LEFT JOIN `users` AS u ON c2u.user_plevel = u.plevel");
Goro
  • 519
  • 1
  • 11
  • 30