为另一列的数组元素选择一列的公共值

时间:2012-12-05 06:30:38

标签: mysql sql

我有以下表结构,

--------------------------------------------------------
| id   |   po_bill_details_id   |   po_id   |   status |
--------------------------------------------------------
| 1    |   18                   |   6       |   1      |
| 2    |   16                   |   7       |   1      |
| 3    |   18                   |   7       |   1      |
--------------------------------------------------------

我需要选择po_bill_details_id,它将是给定的po_id数组的公共值。即如果我给[6,7]作为po_id的输入,我应该只从po_bill_details_id获得18而不是16.如何向MySQL查询这个逻辑?

4 个答案:

答案 0 :(得分:2)

尝试以下方法。我在移动设备上,所以无法测试。根据数组中元素的数量,您可以更改计数。

SELECT bill_id FROM yourtable
WHERE PO_id IN (7,6) 
GROUP BY bill_d 
HAVING COUNT(DISTINCT po_id) = 2

答案 1 :(得分:1)

您可以尝试::

select 
po_bill_details_id

from table

where po_id in (/*your array*/)

group by po_bill_details_id having count(po_id)=(/*your array's length*/)

答案 2 :(得分:1)

SELECT po_bill_details_id
FROM   <table>
WHERE  po_id IN ( 6, 7 )
HAVING COUNT(po_bill_details_id) > 1  

答案 3 :(得分:-1)

你的意思是

select distinct po_bill_details_id from YOUR_TABLE where po_id=6 or po_id=7;