简体   繁体   English

如何联接两个表并仅获取特定记录

[英]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 ) 供应商-列( supp_idsupp_codesupp_nameaddress
  2. SelectedSuppliers -- columns ( supplier_id , is_selected , date_a) SelectedSuppliers -列( supplier_idis_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 . 对于供应商表中的特定日期,然后转到SelectedSupplier表。

When I load saved suppliers from SelectedSupplier table I need to view all the suppliers from two tables. 当我从SelectedSupplier表中加载保存的供应商时,我需要从两个表中查看所有供应商。 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 它可以工作,但仅加载SelectedSupplier记录,而不是所有记录

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: 只需颠倒连接顺序,然后将条件移到ON子句中,即可使左连接仍然有效:

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. 通过首先从suppliers选择, 然后退出加入,您将获得每个供应商行。

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!) 注意:通常(并且错误地)认为ON子句可能仅具有与“键相关”的条件,但实际上它可能包含任何条件(甚至与被连接的表都不相关!)

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. 注意:从表名称中创建一个笛卡尔积,其中所有值都可以通过指定where子句从中选择。

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. 无论您要输入is_selected列中的任何信息以了解该信息是否已提供,都将在您的where条件下使用。 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 假设默认情况下为0(如果未选中),并且希望加载这些记录,则只需将其放置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, 尚不清楚为什么要引用shop_list ,当该表或别名未出现在查询中时,或者为什么其中一个表名包含尾随空格,

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. 根据您的描述,我认为您希望supplier位于LEFT JOIN的左侧(以返回Supplier表中的所有行。

(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.) (我shop_list的引用替换为对SelectedSuppliers的引用。我在表名上留下了尾随空格,就像在示例查询中一样。)

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. 所有行都将从左侧的表(在此查询中为supplier )返回,而右侧表的所有匹配行都将返回。

The value for is_selected in will be NULL when there is no matching row in the SelectedSuppliers table. 如果SelectedSuppliers表中没有匹配的行,则is_selected in的值将为NULL。 This can easily be replaced with a 0, if that's desired, by wrapping that in an IFNULL function. 如果需要,可以通过将其包装在IFNULL函数中轻松地将其替换为0。

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, 这将返回supplier行,其中SelectedSuppliers表中没有匹配的行,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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