I have a table with fields id
, text
, and title
. There may be multiple rows with the same title
(title
is not unique). Additionally, id
is the primary key.
I need only to know if there are at least one row with title
="A" that contains the string "aaa" in text
, and additionally at least one row with title
="B" that contains the string "bbb" in text
.
This is what I tried:
mysql> SELECT (SELECT text FROM table WHERE title = 'A') as aText, (SELECT text FROM table WHERE title = 'B') as bText;
I had planned on parsing the values of aText
and bText
for the strings "aaa" and "bbb" respectfully with PHP. However, I have two issues:
1) Major issue: Due to title
not being unique, the subqueries may return multiple rows. MySQL breaks on that happening with this error:
ERROR 1242 (21000): Subquery returns more than 1 row
2) Minor issue: The reason that I am parsing in PHP is to avoid using MySQL's LIKE operator. Would I be better off doing the parsing of the string right there in MySQL as such:
mysql> SELECT (SELECT text FROM table WHERE title = 'A' AND text LIKE '%aaa%') as aText, (SELECT text FROM table WHERE title = 'B' AND text LIKE '%bbb%') as bText;