I make a simple query:

SELECT cars.id, cars.firma, cars.model, cars.cost, sales.sale FROM cars LEFT JOIN sales ON cars.id = sales.id_cars; 

The cars table has an id field and the sales table has an id field.

Further in the code loop, when I write $line['id'] the second table field is taken ...

$line['cars.id'] does not help ...

How to explicitly indicate which id field I need ?????

  • Perhaps you should put all the code without trying to simplify it, because there is not enough data in the question. - Razzwan 9:39 pm

2 answers 2

You have a mistake in the question. Because in this form, the key id only one. But you can use the AS operator if you want to get a key with the same name in different variables. For example:

 SELECT cars.id AS cars_id, cars.firma, cars.model, firma.id AS firma_id, FROM cars LEFT JOIN firma ON cars.id = firma.cars_id; 

After such a sample, you can get the value of cars.id using the cars_id key, and firma.id using the key firma_id . This is how it should look like:

 $cars_id = $result['cars_id']; 

In order to avoid such problems in the future, it is necessary to name the columns in the table, starting with the prefix, which is an abbreviation of the name of the table itself. For example, all the columns in the cars table should start with the prefix car_ . For example: car_id , car_name ...

  • And if the query looks like SELECT * FROM cars LEFT JOIN firma ON cars.id = firma.cars_id; - Alexander
  • @ Alexander, then you need to change it to the one you wrote originally. Select specific columns and change their identifiers as I wrote in my answer. Still, it is necessary to name the columns in the table, starting with the name of the table. - Razzwan
  • Received! Thank you very much! - Alexander

I think you have no firm.id in your firm.id . There are only cars.id , sales.id_cars and, following from your question, sales.id . firm tables are not here ...

  • $ line ['cars.id'] does not help - Alexander
  • Well, you can try this: select cars.id from (SELECT * FROM cars LEFT JOIN firma ON cars.id = firma.cars_id); But it is better as advised above to you :) - nobody