I would be forever in debt to the one person that can help me with this case / issue that I'm having:
There is a table with the following structure:
id(AI)| ColA | ColB | ColC
-----------------------------
1 |1 | lorem | ipsum
2 |1 | dolor | sit
3 |0 | amet | nula
...
I would like to set the columns to search in (ColB or ColC) based on the value in the ColA. For example (pseudo query):
Basically, in the same one query:
Let's say that ColB stores article titles and ColC stores articles descriptions. ColA stores articles types. So, if article type is 1, search only in title else, if article type is 0 then search in title and description.
Thank you in advance for your help!
You could do it with conditions in the WHERE
clause SQL Switch/Case in 'where' clause
SELECT *
FROM table
WHERE
(ColA = 1 AND ColB LIKE '%some_criteria%')
OR
(ColA = 0 AND (ColB LIKE '%some_criteria%' OR ColC LIKE '%some_criteria%'))
Also, a simple UNION
would work as another example https://www.w3schools.com/sql/sql_union.asp
SELECT *
FROM table
WHERE ColA = 1 AND ColB LIKE '%some_criteria%'
UNION ALL
SELECT *
FROM table
WHERE ColA = 0 AND (ColB LIKE '%some_criteria%' OR ColC LIKE '%some_criteria%')
Note that UNION ALL
would return duplicates if any were to occur, and just UNION
returns distinct rows.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.