Here i need to select all data in table dn_buys while need to get the sold_date also (if a sale record is avaiaible) from dn_sales table for that relevant vehicle_id. if not avaiaible, the print will have sold_date column blank.
i tried this.
$query = $db->prepare("SELECT *
FROM dn_buys
LEFT JOIN dn_sales
ON dn_buys.vehicle_id = dn_sales.vehicle_id
ORDER BY dn_buys.vehicle_id DESC");
The result is, this correctly featch all data in table_buys and the relevent sales record from dn_sales table for which ever the avaiaible records.
but problem is this does not print the vehicle id of records which are present in both tables. the vehicle id column is blank in results. please advise.
Exactly. Without more explicit identification the question is ambiguous. As humans we can be great at âknowingâ what was meant without the explicitness. Code, not so much. Itâs great at doing what itâs told to do, but it has to be told. For example, instead of
SELECT *
FROM some_table
JOIN other_table
I find it is often better more like
SELECT some_table.some_field
, other_table.other_field
FROM some_table
JOIN other_table
True, it can get very verbose compared to using the * âeverythingâ but it makes the code easier to read and is less work for the database because it doesnât need to figure out what the everything is.
tried INNTER JOIN. It creates a result set from records found in both table only. which ever record not present in either one of the tables are not showing. And UNION also seems not the solution here.
Iâm missing something. You donât want results with missing values in them and you donât want only results that donât have missing values in them. How would you describe what your ideal âexpected resultsâ would be?
-yah i want all records in dn_buys
-but some of the above above records also have more detail in dn_sales table.
unique key is vehicle_id. so these more details should also be added to the result set.
absolutly. so i have decided to join both table data togeather and make a single table called âvehcilesâ where brought and sold records all are kept in one table. easy to manage.
this i am reparing another once developed system