1

Am having the following table structure,

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

I need to select po_bill_details_id, which will be a common value for the given array of po_id. i.e. If i give [6,7] as input for po_id, i should only get 18 from po_bill_details_id and not 16. How can i query to MySQL for this logic?

1
  • So you want to get bill id which has corresponding PO id as per entire input array. Interesting. Commented Dec 5, 2012 at 6:36

4 Answers 4

2

Try the following. I am on mobile, so can't test out though. Based on the number of elements in array you can change the count.

SELECT bill_id FROM yourtable
WHERE PO_id IN (7,6) 
GROUP BY bill_d 
HAVING COUNT(DISTINCT po_id) = 2
Sign up to request clarification or add additional context in comments.

Comments

1

You can try ::

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*/)

3 Comments

That should be having count(po_id) > 1, no? I think the OP wants results that are duplicates once the table has been filtered by the predicates.
@Asad: You have answered to a a specific example OP has provided, the OP needed those duplicate values which are common to all po_id
the OP needed those duplicate values which are common to all po_id this is the part I'm having trouble understanding. By the way, po_bills_id isn't a column.
1
SELECT po_bill_details_id
FROM   <table>
WHERE  po_id IN ( 6, 7 )
HAVING COUNT(po_bill_details_id) > 1  

Comments

-1

Do you mean

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.