I have an SQL field defined as set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')
Now I want to run a query where I pass for example nightlife,food and I want the result to contain ALL records where the Category contains nightlife or food. So for example a record with nightlife,culture,sports should also be returned as it contains nightlife. I'm not sure how to run this. I tried using the IN keyword in the following way:
'SELECT ... FROM table WHERE '$category' IN Categories
however this isn't working.
UPDATE
Running following query as suggested in answer:
SELECT *
FROM images
WHERE id = '2650225'
AND WHERE FIND_IN_SET('sports', Categories ) >0
OR FIND_IN_SET('nightlife', Categories ) >0
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0
receiving following error :
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE find_in_set('sports', Categories) > 0 or find_in_set('nightlife', Categori' at line 1
You can use find_in_set with or:
select *
from yourtable
where find_in_set('nightlife', categories) > 0 or
find_in_set('food', categories) > 0
SQL Fiddle Demo
Based on your edits, you can't have multiple where clauses. Also you need to use parentheses around the or criteria:
SELECT *
FROM images
WHERE id = '2650225' AND
(FIND_IN_SET('sports', Categories ) > 0 OR
FIND_IN_SET('nightlife', Categories ) >0)
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0
Related
Trying to get a list of the latest forum posts from a user, ordered by the date it was made.
The problem is posts are split between topics and replies, so i need to get the reply text from a different table if the latest post isn't the topic itself.
Trying to use CASE to switch between a subquery if it's a reply else use the topic text.
SELECT
t.`topic_id`,
t.`topic_title`,
t.`last_post_date`,
t.`last_post_id`,
CASE WHEN t.`replys` > 0 THEN (
SELECT
`reply_text` AS 'text'
FROM
`forum_replies`
WHERE
`post_id` = t.`last_post_id`
) ELSE t.`topic_text` AS 'text'
END
FROM
`forum_topics` t
WHERE
t.`approved` = 1 AND t.`forum_id` IN (1) AND t.last_post_user_id = 1
ORDER BY
t.`last_post_date`
DESC
LIMIT 5
The error is:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'AS 'text' END FROM
forum_topics t WHERE
t.approved = 1 AND t.`f' at line 13
Use of subquery in CASE is not allowed. Use LEFT JOIN instead:
SELECT
t.`topic_id`,
t.`topic_title`,
t.`last_post_date`,
t.`last_post_id`,
t.`reply_text` as 'text'
FROM
`forum_topics` t
LEFT JOIN `forum_replies` r on r.`post_id` = t.`last_post_id`
WHERE
t.`approved` = 1 AND t.`forum_id` IN (1) AND t.last_post_user_id = 1
ORDER BY
t.`last_post_date`
DESC
LIMIT 5
So basically, I am trying to select a row from my database, based on two values, I have tried doing
SELECT FROM users WHERE first = 'Bob' AND last = 'Stevenson';
but when I do it it gives this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM users WHERE first = 'Robert' AND last = 'Westbury'' at line 1
Thanks in advance,
Robert
You need to specify what column(s) do you want to select, SELECT statement should be followed by the column names or by * to select all the columns:
SELECT * FROM users WHERE first = 'Bob' AND last = 'Stevenson'
Dude, it's Select (* OR <column_name_1>, <column_name_2>... ) From <table_name> where <condition>;
I want remove duplicate record & use this query:
DELETE FROM news e
GROUP BY e.itemId, e.tag_id
HAVING COUNT(e.itemId) > 1
AND COUNT(e.tag_id) > 1
but get this error:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'e
How can I do this?
I'm not sure what you're trying to acheive here, please expand your explanation to add a little more info. From what i see you actually need to create a sub query as you can't use GROUP BY directly on the delete, try something like this:
delete from table
where columnA in (
select columnA
from (
select columnA
from YourTable
group by columnA
having count(*) > 1
) t
)
Not exactly tailored to your problem but you should get the idea.
for remove duplicate records, use this query
DELETE
n1
FROM
news n1,
news n2
WHERE
n1.id < n2.id
AND n1.itemId = n2.itemId
AND n1.tag_id = n2.tag_id
AND n1.tag_id IS NOT NULL
I am trying to COUNT rows less or greater than 100 in MYsQL table using PHPMyAdmin
SELECT HAVING COUNT(`roadLength`) > 100 AS stop3 FROM `single-ecolo-dis-yes-tbl`
and
SELECT HAVING COUNT(`roadLength`)< 100 FROM `single-ecolo-dis-yes-tbl`
but I am getting this error
SELECT HAVING COUNT(`roadLength`) > 100 FROM `single-ecolo-dis-yes-tbl`
LIMIT 0, 25
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'HAVING COUNT(`roadLength`) > 100 FROM `single-ecolo-dis-yes-tbl`
LIMIT 0, 25' at line 1
can you please let me know why this is happening? and how I can fix this?
having is to reduce the groups in a query and has no business in the select clause. It is a clause for itself
SELECT SUM(`roadLength` > 100) AS stop3
FROM `single-ecolo-dis-yes-tbl`
Normally, for a simple count like this, you would put the condition in the WHERE clause:
SELECT COUNT(*) as stop3
FROM `single-ecolo-dis-yes-tbl` t
WHERE t.roadLength > 100 ;
This allows the optimizer to use an index on roadLength, if one is available.
I am trying to return a number based on the count of results from a table and to avoid having to count the results twice in the IF statement I am using a subquery. However I get a syntax error when trying to run the query, the subquery I have tested by itself runs fine.
Any ideas what is wrong with the query? The syntax looks correct to me
SELECT IF(daily_count>8000,0,IF(daily_count>6000,1,2))
FROM (
SELECT count(*) as daily_count
FROM orders201003
WHERE DATE_FORMAT(date_sub(curdate(), INTERVAL 1 DAY),"%d-%m-%y") =
DATE_FORMAT(reqDate,"%d-%m-%y")
) q
Error message I get is:
You have an error in your SQL syntax.
Check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'SELECT
count(*) as daily_count
FROM orders201003
SELECT CASE WHEN daily_count > 8000 THEN 0 WHEN daily_count > 6000 THEN 1 ELSE 2 END
FROM (
SELECT count(*) as daily_count
FROM orders201003
WHERE DATE_FORMAT(date_sub(curdate(), INTERVAL 1 DAY),"%d-%m-%y") =
DATE_FORMAT(reqDate,"%d-%m-%y")
) AS q
Also note that the nested queries are only supported starting from MySQL 4.1.