假设我们有下表:
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: info
channels: ["!event"]
max_files: 10
include_stacktraces: true <--- SET TO TRUE
我们如何获得tbl_item_id,它具有所有提供的行的所有正确列数据?
|--------|-------------|------------|------------|------------|------------|
| tbl_id | tbl_item_id | tbl_data_a | tbl_data_b | tbl_data_c | tbl_data_d |
|--------|-------------|------------|------------|------------|------------|
| 1 | 1 | a1 | b1 | c1 | d1 |
| 2 | 1 | a2 | b2 | c2 | d2 |
| 3 | 1 | a3 | b3 | c3 | d3 |
| 4 | 2 | a1 | b1 | c1 | d1 |
| 5 | 2 | a2 | b2 | c2 | d2 |
| 6 | 3 | a1 | b1 | c1 | d1 |
| 7 | 4 | a1 | b1 | c1 | d1 |
| 8 | 4 | a2 | b2 | c22 | d2 |
|--------|-------------|------------|------------|------------|------------|
对于这些数据,我们为每个tbl_item_id提供以下内容:
因此,结果必须包含ID 1和2。
答案 0 :(得分:0)
请尝试以下操作:
SELECT tbl_item_id
FROM your_table
GROUP BY tbl_item_id
HAVING
SUM(tbl_data_a = 'a1' AND tbl_data_b = 'b1' AND tbl_data_c = 'c1' AND tbl_data_d = 'd1')
AND
SUM(tbl_data_a = 'a2' AND tbl_data_b = 'b2' AND tbl_data_c = 'c2' AND tbl_data_d = 'd2')
答案 1 :(得分:0)
在MySQL中,您可以将查询构造为:
SELECT tbl_item_id
FROM t
WHERE (tbl_data_a, tbl_data_b, tbl_data_c, tbl_data_d) IN
( ('a1', 'b1', 'c1', 'd1'), ('a2', 'b2', 'c2', 'd2') )
GROUP BY tbl_item_id
HAVING COUNT(*) = 2;
这假定您的表没有重复项。
答案 2 :(得分:0)
select yt1.tbl_item_id from your_table yt1
inner join your_table yt2 on yt1.tbl_item_id = yt2.tbl_item_id
where yt1.tbl_data_a = 'a1' and yt1.tbl_data_b = 'b1' and yt1.tbl_data_c = 'c1' and yt1.tbl_data_d = 'd1'
and yt2.tbl_data_a = 'a2' and yt2.tbl_data_b = 'b2' and yt2.tbl_data_c = 'c2' and yt2.tbl_data_d = 'd2'