How to join two tables and get specific records only?
I have two tables
- Supplier — Columns (
supp_id,supp_code,supp_name,address) - SelectedSuppliers — columns (
supplier_id,is_selected, date_a)
I load the all the suppliers to grid view and select specific suppliers by check box
For specific date from supplier table then it goes to SelectedSupplier table .
When I load saved suppliers from SelectedSupplier table I need to view all the suppliers from two tables. Which means if I added a new supplier it should be display when I’m loading second time .
This is my query
SELECT
`supplier`.`supp_id`,
`supplier`.`supp_name`,
`supplier`.`address`,
`supplier`.`supp_code`,
`SelectedSuppliers `.`is_selected`
FROM
`SelectedSuppliers `
LEFT JOIN
`supplier` ON (`shop_list`.`supplier_id` = `supplier`.`supp_id`)
WHERE
SelectedSuppliers.date_a = '2013-1-5'
It works but load SelectedSupplier records only not all records
Thanks.
It’s not at all clear why you have a reference to
shop_list, when that table or alias doesn’t appear in your query, or why one of your table names includes a trailing space,From your description, I think you want
supplierto be on the left side of the LEFT JOIN (to return all rows from the supplier table.(I replaced the reference to
shop_listwith a reference to theSelectedSuppliers. I left the trailing space on the table name, as in your example query.)All rows will be returned from the table on the left side (in this query, that’s
supplier), along with any matching rows from the table on the right side.The value for
is_selectedin will be NULL when there is no matching row in the SelectedSuppliers table. This can easily be replaced with a 0, if that’s desired, by wrapping that in an IFNULL function.If your intent is to EXCLUDE suppliers that are already selected, we can add a simple predicate to the query to eliminate the matching rows:
That will return rows from
supplierwhere there is no matching row in theSelectedSupplierstable,