Below is an excerpt from an SQL query I am doing with php and it’s working as I expected and wanted however for my own curiosity’s sake I want to know how to only select the data I need in a multi-table query.
select * from item_inventory, order_form_items where item_inventory.item_inventory_id = "1796" and order_form_items.order_form_item_id = "7" OR item_inventory.item_inventory_id = "2785" and order_form_items.order_form_item_id = "6" OR item_inventory.item_inventory_id = "2602" and order_form_items.order_form_item_id = "5" OR item_inventory.item_inventory_id = "1793" and order_form_items.order_form_item_id = "8" order by item_inventory.item_inventory_title asc
I do no need or want * from both table rows. How would I adjust the query so that I only get * from item_inventory, and say “date_released” from order_form_items?
I have tried to no avail:
select * from item_inventory, date_released order_form_items where
Don’t use the *, instead specify all column names you want to extract.
Good practice in a multiple table query is to specify the columns table as well:
SELECT
table1.column1
, table1.column2
, table2.column3
FROM table1
INNER JOIN table2
ON table1.column = table2.column
WHERE ....
In a simple query like this with only one join there may not be much difference between using an implicit join and an explicit one, but when you start adding other tables the explicit join lets you see immediately the join conditions for each join:
SELECT
table1.column1
, table1.column2
, table2.column3
FROM table1
INNER JOIN table2
ON table1.column5 = table2.column6
INNER JOIN table3
ON table1.column7 = table3.column8
AND table1.column9 = table3.column10
INNER JOIN table4
ON table1.column11 = table3.column12
ecc
WHERE ....