Вы можете использовать этот способ для получения желаемого результата.
SELECT *
FROM TableName
WHERE company IN (SELECT company
FROM TableName
WHERE contact = 'Jim Jones')
В вашем запросе, если вы изменяете WHERE tableB.contact = 'Jim Jones'
на WHERE tableA.contact = 'Jim Jones'
, то вы должны также изменить оператор SELECT.
SELECT tableA.contact_id, tableA.company, tableA.contact
FROM company AS tableA
JOIN company AS tableB
ON tableA.company = tableB.company
WHERE tableB.contact = 'Jim Jones'
SELECT tableB.contact_id, tableB.company, tableB.contact
FROM company AS tableA
JOIN company AS tableB
ON tableA.company = tableB.company
WHERE tableA.contact = 'Jim Jones'
Вы должны сделать это, потому что вы фильтруете по этому результату.
(Вы делаете самостоятельное объединение, используя столбец company
.)
SELECT *
FROM company AS tableA
JOIN company AS tableB
ON tableA.company = tableB.company
+-----------------+----------------+---------------------+----------------+----------------+---------------------+
| contact_id (A) | company (A) | contact (A) | contact_id (B) | company (B) | contact (B) |
+-----------------+----------------+---------------------+----------------+----------------+---------------------+
| 1000000001 | Village Toys | John Smith | 1000000001 | Village Toys | John Smith |
| 1000000002 | Kids Place | Michelle Green | 1000000002 | Kids Place | Michelle Green |
| 1000000003 | Fun4All | Jim Jones | 1000000003 | Fun4All | Jim Jones |
| 1000000004 | Fun4All | Denise L. Stephens | 1000000003 | Fun4All | Jim Jones |
| 1000000003 | Fun4All | Jim Jones | 1000000004 | Fun4All | Denise L. Stephens |
| 1000000004 | Fun4All | Denise L. Stephens | 1000000004 | Fun4All | Denise L. Stephens |
| 1000000005 | The Toy Store | Kim Howard | 1000000005 | The Toy Store | Kim Howard |
+-----------------+----------------+---------------------+----------------+----------------+---------------------+