Comparing a subquery with another subquery

Asked

Viewed 229 times

2

I have two tables, table A and table B, each with a column called text. Both tables have equal records to some extent, up to the character '='. What I want to do is a SELECT of all records in table A where the records up to the character '=' of it are equal to the records up to the character '=' of table B. I tried this here, only it gives problem of subquery only being able to return a value.

SELECT texto FROM A
WHERE (SELECT LEFT(texto, POSITION("=" IN texto)) FROM A) = (SELECT LEFT(texto, POSITION("=" IN texto)) FROM B);

2 answers

0

To get this result it is necessary to make a INNER JOIN:

SELECT A.`texto` FROM `A`
INNER JOIN `B` ON SUBSTR(A.`texto`, 1, POSITION("=" IN A.`texto`) - 1) = SUBSTR(B.`texto`, 1, POSITION("=" IN B.`texto`) - 1);

0


Instead of subquery, you can make a INNER JOIN with GROUP BY:

SELECT A.texto FROM A
INNER JOIN B
ON
(LEFT(A.texto, POSITION("=" IN A.texto)) = LEFT(B.texto, POSITION("=" IN B.texto)))
GROUP BY A.texto

To use subquery, use IN in this way:

SELECT texto FROM A
WHERE LEFT(texto, POSITION('=' IN texto))
IN
(SELECT LEFT(texto, POSITION('=' IN texto)) FROM B)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.