简体   繁体   中英

How to join two tables and get specific records only

How to join two tables and get specific records only?

I have two tables

  1. Supplier -- Columns ( supp_id , supp_code , supp_name , address )
  2. 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.

SELECT
  `supplier`.`supp_id`,
  `supplier`.`supp_name`,
  `supplier`.`address`,
  `supplier`.`supp_code`,
  `SelectedSuppliers `.`is_selected`
FROM 'supplier',`SelectedSuppliers`
  LEFT JOIN `supplier`
    ON (`shop_list`.`supplier_id` = `supplier`.`supp_id`)
where SelectedSuppliers.date_a = '2013-1-5'

Please have a look at the image 在此处输入图片说明

Just reverse the join order and move the condition into the ON clause to allow the left join to still work:

SELECT
  `supplier`.`supp_id`,
  `supplier`.`supp_name`,
  `supplier`.`address`,
  `supplier`.`supp_code`,
  `SelectedSuppliers `.`is_selected`
FROM `supplier`
LEFT JOIN `SelectedSuppliers ` 
    ON `shop_list`.`supplier_id` = `supplier`.`supp_id`
    AND SelectedSuppliers.date_a = '2013-1-5'

By selecting from suppliers first, then left joining you'll get every supplier row.

Note: It is commonly (and incorrectly) believed that the ON clause may only have "key related" conditions, but in fact it may contain any condition (even one not related to the tables being joined!)

Use this form

SELECT A1,A2,...
FROM TABLE1,TABLE2,TABLE3,....
WHERE ......

Note: From table names creates a cartesian product of all the values out of which you choose by specifying the where clause.

whatever information you are putting into is_selected column to know whether that information has been supplied or not use that in your where condition. Suppose by default it is 0 (if that is not selected) and you want those records to be loaded then simply put where SelectedSuppliers.is_selected = 0

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 supplier to be on the left side of the LEFT JOIN (to return all rows from the supplier table.

(I replaced the reference to shop_list with a reference to the SelectedSuppliers . I left the trailing space on the table name, as in your example query.)

SELECT s.supp_id
     , s.supp_name
     , s.address
     , s.supp_code
     , e.is_selected
  FROM `supplier` s
  LEFT 
  JOIN `SelectedSuppliers ` e
    ON e.supplier_id = s.supp_id
   AND e.date_a = '2013-1-5'

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_selected in 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:

WHERE e.supplier_id IS NULL 

That will return rows from supplier where there is no matching row in the SelectedSuppliers table,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM