Skip to content Skip to sidebar Skip to footer

Mysql - Using Results From One Query In Another Query

I have this query that gives me back rows with two columns each containing an id. SELECT idEng, idDutch FROM phraseConnections WHERE cat = '3' the id's are from other tables where

Solution 1:

Better use Joins:

SELECT
   a.phrase,
   b.phrase
FROM
   phraseConnections pc
INNER JOIN
   phraseEnglish AS a
ON
   pc.idEng = a.id
INNER JOIN
   phraseDutch AS b
ON
   pc.idDutch = b.id
WHERE
   pc.cat = 3;

If you want records that have no corresponding row in one (or both) language too then you could use outer joins.

Solution 2:

The error must be because of inner query returns more than one value.

Try using IN:

SELECT a.phrase, b.phrase
FROM phraseEnglish as a, phraseDutch as b
WHERE a.id IN (SELECT idEng
           FROM phraseConnections
           WHERE cat = '3')and b.id IN (SELECT idDutch
          FROM phraseConnections
          WHERE cat = '3')

Solution 3:

Your problem is probably because You can't ask to have a single value equal a set of multiple values. To correct this in your SQL, use In.

SELECT a.phrase, b.phrase
FROM phraseEnglish as a, phraseDutch as b
WHERE a.id In 
        (SELECT idEng
         FROM phraseConnections
         WHERE cat = '3')and b.id In
        (SELECT idDutch
         FROM phraseConnections
         WHERE cat = '3')

However, this query can be done without subqueries:

SELECT e.phrase, d.phrase
FROM phraseConnections c
   join  phraseEnglish e 
     on e.id = c.idEng
   join  phraseDutch d 
     on d.id = c.idDutch
Where e.Cat = '3'

Post a Comment for "Mysql - Using Results From One Query In Another Query"