所以我很难在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。现在记录是重复的。它显示了9条记录,而不是显示3条记录。我有任何错误吗?
答案 0 :(得分:0)
从您的评论中,我相信comparitive_st_tech_compliance对于单个tender_id,在comparitive_st_sup表中的行中有多行。 如果是这样,那么它将返回多行并不重要你使用哪个Join。
答案 1 :(得分:0)
您可能需要这样做:
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
答案 2 :(得分:0)
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;
答案 3 :(得分:0)
如果您不想或不能GROUP BY
,您可以尝试子查询。您还可以在子查询中ORDER BY
日期/ 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
<强> LE 强>
IF和ONLY如果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
但是如果sl *与表* _st_sup和* _tech_compliance不同,则需要在tender_id旁边的产品和注释之间添加关系
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。 supplier_name您需要更新所有表。现在假设您愿意进行更改,我建议将您的数据拆分为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个表仅因技术评论和产品说明而不同