在 PostgreSQL 中从 table 获取客户名称
Get customer name from table in PostgreSQL
SELECT partner_id
FROM parking_test
WHERE test_date=CURRENT_DATE
使用此查询,我们可以获得今天参加考试的合作伙伴的详细信息。
客户名称出现在另一个 table res_partner
:
SELECT name FROM res_partner rs
我试过以下代码:
SELECT
name, partner_id
FROM
(SELECT rs.name FROM res_partner rs) rs
LEFT JOIN
(SELECT partner_id FROM parking_test) pl ON rs.id = pl.ipartner_id
WHERE
test_date = CURRENT_DATE
但是我得到一个错误
ERROR: column rs.id does not exist LINE 1: ...t join (select partner_id from parking_test)pl on rs.id=pl.i...
res_partner
table:
+------------+-------+
| id | name |
+------------+-------+
| 34567 | XYZ1 |
| 34568 | XYZ2 |
| 34569 | DDHC |
| 34566 | DVDV |
| 34570 | DVFDV |
| 34576 | FVFV |
| 34567 | FVV |
+------------+-------+
parking_test
table
+------------+-----------+
| Partner_id | test_date |
+------------+-----------+
| 34567 | 11/06/2021|
| 34568 | 11/06/2021|
| 34569 | 12/06/2021|
| 34566 | 13/06/2021|
| 34570 | 14/06/2021|
| 34576 | 15/06/2021|
| 34567 | 16/06/2021|
+------------+-----------+
因为我们正在检查当前日期,所以我想得到 XYZ1、XYZ2 的答案
使用 INNER JOIN 并在 WHERE 子句中添加条件
-- PostgreSQL
SELECT rs.name, pt.partner_id
FROM res_partner rs
INNER JOIN parking_test pt
ON rs.id = pt.partner_id
WHERE pt.test_date= CURRENT_DATE
SELECT partner_id
FROM parking_test
WHERE test_date=CURRENT_DATE
使用此查询,我们可以获得今天参加考试的合作伙伴的详细信息。
客户名称出现在另一个 table res_partner
:
SELECT name FROM res_partner rs
我试过以下代码:
SELECT
name, partner_id
FROM
(SELECT rs.name FROM res_partner rs) rs
LEFT JOIN
(SELECT partner_id FROM parking_test) pl ON rs.id = pl.ipartner_id
WHERE
test_date = CURRENT_DATE
但是我得到一个错误
ERROR: column rs.id does not exist LINE 1: ...t join (select partner_id from parking_test)pl on rs.id=pl.i...
res_partner
table:
+------------+-------+
| id | name |
+------------+-------+
| 34567 | XYZ1 |
| 34568 | XYZ2 |
| 34569 | DDHC |
| 34566 | DVDV |
| 34570 | DVFDV |
| 34576 | FVFV |
| 34567 | FVV |
+------------+-------+
parking_test
table
+------------+-----------+
| Partner_id | test_date |
+------------+-----------+
| 34567 | 11/06/2021|
| 34568 | 11/06/2021|
| 34569 | 12/06/2021|
| 34566 | 13/06/2021|
| 34570 | 14/06/2021|
| 34576 | 15/06/2021|
| 34567 | 16/06/2021|
+------------+-----------+
因为我们正在检查当前日期,所以我想得到 XYZ1、XYZ2 的答案
使用 INNER JOIN 并在 WHERE 子句中添加条件
-- PostgreSQL
SELECT rs.name, pt.partner_id
FROM res_partner rs
INNER JOIN parking_test pt
ON rs.id = pt.partner_id
WHERE pt.test_date= CURRENT_DATE