简体   繁体   中英

PHP query: Select from table with multiple statements

Hello there :) I have a PHP query that selects data from a database, and so far it doesn't seem to return any results.

$query = $db->query("SELECT * FROM table1 WHERE time_finished IS NULL AND WHERE comment LIKE 'A%' OR comment LIKE 'B%' OR comment LIKE 'C%' ");

Here is the query at the moment. What it is trying to do is select the table where the column "time_finished" is null AND where the comment is either beginning with A, B or C.

Now if I remove the statements after the NULL statement, it returns all rows where the "time_finished" is NULL. However, after the NULL, I get no results at all.

Now I'm guessing this has something to do with the way I've written or structured it, or possibly I've set up the "Begin with" part wrong.

Any contributors to this post, I thank you for your help :)

You have an extra WHERE in the query. It should be like:

SELECT * 
FROM table1 
WHERE time_finished IS NULL 
  AND (comment LIKE 'A%' 
       OR comment LIKE 'B%' 
       OR comment LIKE 'C%')

Try this remove extra WHERE :-

SELECT * FROM table1 WHERE time_finished IS NULL 
AND (comment LIKE 'A%' OR comment LIKE 'B%' OR comment LIKE 'C%')

Remove duplicate WHERE clause and added OR conditions inside () brackets.

SELECT *
FROM table1
WHERE time_finished IS NULL 
AND
(
comment LIKE 'A%'
OR comment LIKE 'B%'
OR comment LIKE 'C%'
)

You have an extra where in query. Please remove and then try :

SELECT * FROM table1 WHERE time_finished IS NULL 
AND (comment LIKE 'A%' OR comment LIKE 'B%' OR comment LIKE 'C%')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM