在 SQL PHP 中加入 table 无法正常工作
JOIN table in SQL PHP not working perfectly
我正在尝试 select 没有连接和任何关系的多个表。一切正常,但我遇到了一个问题,即结果输出重复。
我试图用图表来表示。
`table_in`
|------|------------------|--------------|-------|
| ID | component_in_key | insert_date | type |
|------|------------------|--------------|-------|
| 1 | 123456789 | 2021-02-01 | I |
--------------------------------------------------
`table_request`
|------|-------------------|--------------|-------|
| ID | component_req_key | insert_date | type |
|------|-------------------|--------------|-------|
| 1 | 123456789 | 2021-02-02 | R |
| 2 | 123456789 | 2021-02-03 | R |
| 3 | 123456789 | 2021-02-04 | R |
---------------------------------------------------
`table_approve`
|------|-------------------|--------------|-------|
| ID | component_apv_key | insert_date | type |
|------|-------------------|--------------|-------|
| 1 | 123456789 | 2021-02-05 | A |
| 2 | 123456789 | 2021-02-07 | A |
| 3 | 123456789 | 2021-02-08 | A |
---------------------------------------------------
尝试 SQL 是 - SELECT * FROM table_in, table_request, table_approve WHERE table_in.component_in_key = 123456789 AS component AND table_request.component_req_key = 123456789 AND table_approve.component_apv_key= 123456789 ORDER BY table_in.insert_date AND table_request.insert_date AND table_approve.insert_date
OUTPUT table_in.component_in_key
重复 3 次。
寻找输出
|------+---------------+--------------+-------+
| ID | component | insert_date | type |
-----------------------------------------------
| 1 | 123456789 | 2021-02-01 | I |
| 2 | 123456789 | 2021-02-02 | R |
| 3 | 123456789 | 2021-02-03 | R |
| 4 | 123456789 | 2021-02-04 | R |
| 5 | 123456789 | 2021-02-05 | A |
| 6 | 123456789 | 2021-02-07 | A |
| 7 | 123456789 | 2021-02-08 | A |
---------------------------------------------
请帮我看看我是怎么做到的 PHP SQL。
我在过去 3 天里一直在尝试这个,但没有工作,在我的情况下没有 Whosebug 线程工作。
我认为您要查找的是 UNION
查询:
SELECT ID, component_key, insert_date, type
FROM (
SELECT ID, component_in_key AS component_key, insert_date, type
FROM table_in
UNION ALL
SELECT ID, component_req_key, insert_date, type
FROM table_request
UNION ALL
SELECT ID, component_apv_key, insert_date, type
FROM table_approve
) u
WHERE component_key = 123456789
ORDER BY insert_date
输出:
ID component_key insert_date type
1 123456789 2021-02-01 I
1 123456789 2021-02-02 R
2 123456789 2021-02-03 R
3 123456789 2021-02-04 R
1 123456789 2021-02-05 A
2 123456789 2021-02-07 A
3 123456789 2021-02-08 A
我正在尝试 select 没有连接和任何关系的多个表。一切正常,但我遇到了一个问题,即结果输出重复。
我试图用图表来表示。
`table_in`
|------|------------------|--------------|-------|
| ID | component_in_key | insert_date | type |
|------|------------------|--------------|-------|
| 1 | 123456789 | 2021-02-01 | I |
--------------------------------------------------
`table_request`
|------|-------------------|--------------|-------|
| ID | component_req_key | insert_date | type |
|------|-------------------|--------------|-------|
| 1 | 123456789 | 2021-02-02 | R |
| 2 | 123456789 | 2021-02-03 | R |
| 3 | 123456789 | 2021-02-04 | R |
---------------------------------------------------
`table_approve`
|------|-------------------|--------------|-------|
| ID | component_apv_key | insert_date | type |
|------|-------------------|--------------|-------|
| 1 | 123456789 | 2021-02-05 | A |
| 2 | 123456789 | 2021-02-07 | A |
| 3 | 123456789 | 2021-02-08 | A |
---------------------------------------------------
尝试 SQL 是 - SELECT * FROM table_in, table_request, table_approve WHERE table_in.component_in_key = 123456789 AS component AND table_request.component_req_key = 123456789 AND table_approve.component_apv_key= 123456789 ORDER BY table_in.insert_date AND table_request.insert_date AND table_approve.insert_date
OUTPUT table_in.component_in_key
重复 3 次。
寻找输出
|------+---------------+--------------+-------+
| ID | component | insert_date | type |
-----------------------------------------------
| 1 | 123456789 | 2021-02-01 | I |
| 2 | 123456789 | 2021-02-02 | R |
| 3 | 123456789 | 2021-02-03 | R |
| 4 | 123456789 | 2021-02-04 | R |
| 5 | 123456789 | 2021-02-05 | A |
| 6 | 123456789 | 2021-02-07 | A |
| 7 | 123456789 | 2021-02-08 | A |
---------------------------------------------
请帮我看看我是怎么做到的 PHP SQL。 我在过去 3 天里一直在尝试这个,但没有工作,在我的情况下没有 Whosebug 线程工作。
我认为您要查找的是 UNION
查询:
SELECT ID, component_key, insert_date, type
FROM (
SELECT ID, component_in_key AS component_key, insert_date, type
FROM table_in
UNION ALL
SELECT ID, component_req_key, insert_date, type
FROM table_request
UNION ALL
SELECT ID, component_apv_key, insert_date, type
FROM table_approve
) u
WHERE component_key = 123456789
ORDER BY insert_date
输出:
ID component_key insert_date type
1 123456789 2021-02-01 I
1 123456789 2021-02-02 R
2 123456789 2021-02-03 R
3 123456789 2021-02-04 R
1 123456789 2021-02-05 A
2 123456789 2021-02-07 A
3 123456789 2021-02-08 A