Question

I'm trying to search for all entries in one table where they have a column that matches entries in another column that precede a -

Example Output:

enter image description here

This is the query I tried, it returned the error of "Error in query (1242): Subquery returns more than 1 row"

 SELECT * FROM table1 
    WHERE
      table1.Column1 = (
        SELECT
          SUBSTRING_INDEX(table2.Column1,'-',1) 
        FROM
          table2 
        WHERE
          table2.column1 LIKE '%\-%'
      );
Was it helpful?

Solution

You can use IN in your WHERE clause :

SELECT * FROM table1 
WHERE
  table1.Column1 IN (
    SELECT
      SUBSTRING_INDEX(table2.Column1,'-',1) 
    FROM
      table2 
    WHERE
      table2.column1 LIKE '%\-%'
  );

OTHER TIPS

Another way is to use JOIN as

SELECT * FROM table1 t1
inner join (
  SELECT
  SUBSTRING_INDEX(table2.Column1,'-',1) as str
  FROM
  table2 
  WHERE
  table2.column1 LIKE '%\-%'
)t2
on
t1.column1 = t2.str
;

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top