【问题标题】:how to join two tables with a where condition and display the records如何使用 where 条件连接两个表并显示记录
【发布时间】:2015-11-10 20:55:58
【问题描述】:

所以我很难在 Stack 上找到一个好的答案。我正在寻找将两个表上的信息组合在一起的查询。到目前为止,这就是我所拥有的。实际场景如下:

我将尝试从我的角度进行更多解释以实现这一目标:

我有两张桌子:

Comparitive_st_sup
___________________

id  | tender_id | item_name | slno | supplier_name | prod_description
________________________________________________________________________

1       401        Collinear   1      OnlineMetals    Description comes here    
2       401        Filter      2      OnlineMetals    Description comes here
3       401        Antenna     3      OnlineMetals    Description Comes here
4       455        Primer      1      KR Electronics  Description comes here
5       455        Chock       2      KR Electronics  Description comes here


comparitive_st_tech_compliance
_______________________________

id | tender_id | item_name  | slno | supplier_name  | tech_analyst_comment
__________________________________________________________________________

1      401        Collinear    1      OnlineMetals     90%
2      401        Filter       2      OnlineMetals     25%
3      401        Antenna      3      OnlineMetals     87%
4      455        Primer       1      KR Electronics   64%
5      455        Chick        2      KR Electronics   80%

Now i am expecting a result like:

401    Collinear     1    OnlineMetals    Description comes here   90%
401    Filter        2    OnlineMetals    Description comes here   25%
401    Antenna       3    OnlineMetals    Description comes here   87%

根据选择的tender_id,该值作为查询字符串传递,并且必须相应地显示记录。帮助表示赞赏..

我试过了,但结果不正确:

 Select comparitive_st_sup.*,
  comparitive_st_sup.tender_id As tender_id1,
  comparitive_st_sup.supplier_name As supplier_name1,
  comparitive_st_sup.slno As slno1,
  comparitive_st_sup.prod_description As prod_description1,
  comparitive_st_sup.item_name As item_name1,
  comparitive_st_sup.total As total1,
  comparitive_st_tech_compliance.tech_analyst_comment
  From comparitive_st_tech_compliance
  Right Join comparitive_st_sup On comparitive_st_sup.tender_id =
  comparitive_st_tech_compliance.tender_id
  Where comparitive_st_sup.tender_id = 401     

我需要显示 comparitive_st_sup 中的所有字段,并且只显示一个字段 (tech_analyst_comment) 的 where 条件为tender_id。现在记录是重复的。不是显示 3 条记录,而是显示 9 条记录。我有什么错误吗?

【问题讨论】:

  • 您确定要使用RIGHT JOIN 吗?
  • 我可以通过内部连接来实现吗?
  • 不,我没有得到想要的输出。 Right Join 和 Inner Join 显示相同的输出

标签: php mysql


【解决方案1】:

如果您不想或不能GROUP BY,您可以尝试子查询。也可以在子查询中ORDER BYdate/id。

SELECT
    cs.*,
    cs.tender_id AS tender_id1,
    cs.supplier_name AS supplier_name1,
    cs.slno AS slno1,
    cs.prod_description AS prod_description1,
    cs.item_name AS item_name1,
    cs.total AS total1,
    (
        SELECT
            ct.tech_analyst_comment
        FROM comparitive_st_tech_compliance AS ct
        WHERE ct.tender_id = cs.tender_id
        LIMIT 1
    ) AS tech_analyst_comment
FROM comparitive_st_sup AS cs
WHERE cs.tender_id = 401

乐: IF 和 ONLY IF slno 在你的两个表中是相同的标识符 (comparitive_st_sup.slno = comparitive_st_tech_compliance.slno),然后你可以使用额外的连接参数 AND comparitive_st_sup.slno = comparitive_st_tech_compliance.slno 加入它们,所以你的初始查询看起来像这样:

Select comparitive_st_sup.*,
  comparitive_st_sup.tender_id As tender_id1,
  comparitive_st_sup.supplier_name As supplier_name1,
  comparitive_st_sup.slno As slno1,
  comparitive_st_sup.prod_description As prod_description1,
  comparitive_st_sup.item_name As item_name1,
  comparitive_st_sup.total As total1,
  comparitive_st_tech_compliance.tech_analyst_comment
From comparitive_st_tech_compliance
Right Join comparitive_st_sup On
    comparitive_st_sup.tender_id = comparitive_st_tech_compliance.tender_id AND
    comparitive_st_sup.slno = comparitive_st_tech_compliance.slno
Where comparitive_st_sup.tender_id = 401

但如果表 *_st_sup 和 *_tech_compliance 中的 slno 不同,则您需要在招标 ID 旁边添加产品和 cmets 之间的关系

comparitive_st_tech_compliance
+-----------------------------------------------------------------------------------------+
| id | tender_id | product_id | item_name  | slno | supplier_name  | tech_analyst_comment |
+-----------------------------------------------------------------------------------------+
|  1 | 401       |     1      | Collinear  |   1  | OnlineMetals   | description          |
+-----------------------------------------------------------------------------------------+

comparitive_st_tech_compliance.product_id 是 Comparitive_st_sup.id,这也让我想到我建议您更改数据库架构(结构)

旁注:因此,从您的数据库结构来看,需要指出的一件事是它的设计很糟糕。您在两个表格中都有重复的字段,因此如果您需要更新 ex。供应商名称,您将需要更新所有表格。现在假设您愿意进行更改,我建议将您的数据拆分为 3 个表,而不考虑 slno 可能是 2 个表之间的标识符。

comparative_supplier
+---------------------+
| id | supplier_name  |
+---------------------+
| 1  | OnlineMetals   |
| 2  | KR Electronics |
+---------------------+

comparitive_st_sup
+--------------------------------------------------------------------+
| id | tender_id | supplier_id | slno | item_name | prod_description |
+--------------------------------------------------------------------+
| 1  | 401       |      1      |   1  | Collinear | description      |
| 2  | 401       |      1      |   2  | Filter    | description      |
| 3  | 401       |      1      |   3  | Antenna   | description      |
| 4  | 455       |      2      |   1  | Primer    | description      |
| 5  | 455       |      2      |   2  | Chock     | description      |
+--------------------------------------------------------------------+

comparitive_st_tech_compliance
+-----------------------------------------+
| id   | id_supply | tech_analyst_comment |
+-----------------------------------------+
| 15   |     1     |         90%          |
| 56   |     2     |         25%          |
| 123  |     3     |         87%          |
| 412  |     4     |         64%          |
| 684  |     5     |         80%          |
+-----------------------------------------+

使用这种表结构,您可以轻松地加入您的表,而无需重复条目和更改字段,而无需更新所有表。

SELECT
    cs.tender_id, sn.supplier_name, cs.slno, cs.item_name,
    cs.prod_description, ct.tech_analyst_comment
FROM comparitive_st_sup AS cs
LEFT JOIN comparative_supplier AS sn ON sn.id = cs.supplier_id
LEFT JOIN comparitive_st_tech_compliance AS ct ON ct.id_supply = cs.id
WHERE cs.tender_id = 401

或者只是更改您的 st_sup 表并包含技术评论,因为这 2 个表仅在技术评论和产品描述方面有所不同

【讨论】:

  • 这工作正常,只是我认为这个子查询存在问题“SELECT ct.tech_analyst_comment FROM comparitive_st_tech_compliance AS ct WHERE ct.tender_id = cs.tender_id LIMIT 1”... 因为 tech_analyst_comment 不同对于每个tender_id,截至目前,tech_analyst_comment 都显示相同的数字,因为它刚刚取得了第一条记录..
  • 如果您可以编辑您的问题以包含一些表结构和一些数据,您可以获得更好的结果。至于猜测,您是否试图列出技术 cmets 并获取与之相关的数据?可以说:st_sup -> id,tender_id,product_name; st_tech -> id、tender_id、评论;对于特定产品,st_sup 和 st_tech 之间没有关系,只是tender_id,这是我猜测的当前用户之间的关系。因此,您应该在产品和评论之间建立关系,而不是您的初始查询可能会起作用。 st_sup-> pd_id; st_tech->pd_id
  • 我已经用一个可能有效的查询更新了我的答案,也是对您当前数据库架构的建议
  • 太棒了兄弟..它现在工作正常。我创建了一个名为 item_id 的新字段,并在两个表之间建立了关系。它工作得很好……你真的太好了。真的很感谢您抽出宝贵的时间给这个新手。上帝祝福你。再次感谢您....
  • 欢迎您。我也强烈推荐 3 表分开。它更灵活。
【解决方案2】:

从您的评论来看,我相信 comparitive_st_tech_compliance 在 comparitive_st_sup 表中针对单个tender_id 的行有不止一行。 如果是这样,那么无论您使用哪个 Join,它都会返回多行。

【讨论】:

  • comparitive_st_sup 和 comparitive_st_tech_compliance 都有 3 条记录,用于一个单一的招标 ID..
  • 所以这就是为什么它输出 9 条记录而不是 3 条记录?
【解决方案3】:

您可能需要在这里这样做:

Select comparitive_st_sup.*,
  comparitive_st_sup.tender_id As tender_id1,
  comparitive_st_sup.supplier_name As supplier_name1,
  comparitive_st_sup.slno As slno1,
  comparitive_st_sup.prod_description As prod_description1,
  comparitive_st_sup.item_name As item_name1,
  comparitive_st_sup.total As total1,
  comparitive_st_tech_compliance.tech_analyst_comment
  From comparitive_st_tech_compliance
  Where comparitive_st_sup.tender_id = 401  AND comparitive_st_tech_compliance.tender_id =  tender_id1

【讨论】:

  • 现在显示错误“#1051 - Unknown table 'comparitive_st_sup'”
  • 你需要从两个表中选择`From comparitive_st_sup, comparitive_st_tech_compliance`
  • 它显示重复记录..实际上它应该只显示 3 条记录,它现在显示 9 条记录
【解决方案4】:
SELECT comparitive_st_sup.*,
  comparitive_st_sup.tender_id As tender_id1,
  comparitive_st_sup.supplier_name As supplier_name1,
  comparitive_st_sup.slno As slno1,
  comparitive_st_sup.prod_description As prod_description1,
  comparitive_st_sup.item_name As item_name1,
  comparitive_st_sup.total As total1,
  comparitive_st_tech_compliance.tech_analyst_comment
  FROM comparitive_st_sup, comparitive_st_tech_compliance
  WHERE comparitive_st_sup.tender_id = 401
  AND comparitive_st_tech_compliance.tender_id = comparitive_st_sup.tender_id
  GROUP BY comparitive_st_sup.tender_id;

【讨论】:

  • 按tender_id 分组将不起作用,因为会有多个记录具有相同的tender_id。当我运行您的查询时,它只显示一条记录而不是 3 条记录...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
相关资源
最近更新 更多