How to select 1 Row in MySQL Master Table Based on 2 Conditions Existing in a Detailed Table
I have 2 tables -- master and detail
Table A is Master and contains unique rows
ID Name 1 Abc ... 7 Def ... 19 Xyz
Table B is Detail and contains multiple rows per each master row
Ref ID Desc 1 10 abcdef 1 15 ghijk 1 20 lmnop ... 7 05 qrstu 7 10 vwxyz 7 15 ygfdt 7 20 kjqts ... 19 05 htyge 19 15 gqwes
Query Result -- I only wish to select Master Rows that have detailed rows containing both a related detail row of "10" and "15"
So only Rows 1 and Row 7 would be selected.
Thanks for helping.
Here's the shell of my query:
$query_string = ' SELECT a.ID, Name FROM table_a AS a LEFT JOIN table_b AS b ON b.ref = a.ID WHERE b.ID = "10" AND b.ID = "15" ';
SELECT a.ID, Name FROM table_a AS a LEFT JOIN table_b AS b ON b.ref = a.ID WHERE b.ID IN (10,15) group by a.ID having count(distinct b.id) = 2
- A Linux, Apache, MySQL, PHP (LAMP) stack is a combination of open source software that is typically installed together to enable a server to host dynamic […]
- MySQL is an the most popular open-source database management system, in most of hosting company its has been bundle by LAMP(Linux, Apache, MySQL, PHP/Python/Perl) stack. Another alternative […]
- MySQL is the world’s most popular open-source database. Despite its powerful features, MySQL is simple to set up and easy to use. Below are some instructions […]