[英]how to join two tables with a where condition and display the records
所以我很難在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%
根據所選的bidd_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的所有字段,並且僅顯示一個條件條件為bid_id的字段(tech_analyst_comment)。 現在,記錄正在重復。 而不是顯示3條記錄,而是顯示9條記錄。 我有什么錯誤嗎?
從您的評論中,我相信comparitive_st_tech_compliance相對於comparitive_st_sup表中的一行,具有更多的一行,而單個的bid_id。 如果是這樣,那么它將返回多行與您使用哪個Join無關。
您可能需要在這里這樣做:
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
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;
如果您不希望或不能使用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和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不同,則您將需要在bid_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。 供貨商名稱,您將需要更新所有表。 現在假設您願意進行更改,我建議將數據分成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表並包含技術注釋,因為這兩個表僅在技術注釋和產品描述上有所不同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.