MySQL, WHERE to contain separate table - mysql

I've managed to get the first part of my query working fine, it's the final part with the date check that is throwing me.
SELECT * FROM users WHERE available LIKE 'Yes' AND users.id NOT IN (SELECT player_id FROM match_request WHERE club_id LIKE '1000000003') AND (clubs.match_date >= CURRENT_DATE() WHERE clubs.id LIKE '1000000003')
It's this part that I can't work out:
(clubs.match_date >= CURRENT_DATE() WHERE clubs.id LIKE '1000000003')
I get the error message
"#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 clubs.id LIKE '1000000003') LIMIT 0, 25' at line 1"
But I don't know how to fix this.

Replace where with and
SELECT *
FROM users
WHERE available LIKE 'Yes'
AND users.id NOT IN
(
SELECT player_id
FROM match_request
WHERE club_id LIKE '1000000003'
)
AND clubs.match_date >= CURRENT_DATE()
AND clubs.id LIKE '1000000003'
^^^-----here
and you need to join the clubs table if you refer to any columns of that table

Related

can't figure out what's wrong with this sql query

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

MySQL IF() Sub Select

I am having trouble with doing a SUB SELECT in an IF() condition.
I'm trying to get it to select select the MIN saved time if the type does not equal a certain value, excluding that value from the calculation.
Here is my current SQL query that works except for the IF():
SELECT
t.episode,
t.anime_id,
MAX(t.type) as type,
MIN(t.HD) as HD,
MAX(t.thumbnail) as thumbnail,
IF((MAX(t.type)!="raw"),(SELECT MIN(e.saved) FROM anime_episodes e WHERE e.type=MAX(t.type) AND t.episode=e.episode AND t.anime_id=e.anime_id GROUP BY e.anime_id, e.episode), (MIN(t.saved))) as time,
// UP HERE ^
MAX(t.filler) as filler,
m.anime_name,
m.furl,
m.video_thumb
FROM anime_episodes t
LEFT JOIN anime_list m ON
( t.anime_id=m.anime_id )
WHERE t.approved=1 AND t.locked=0 AND m.status=0 AND t.episode>=m.latest_ep
GROUP BY t.anime_id, t.episode
ORDER BY time DESC LIMIT 0,24
Is there something going on with my syntax? It looks fine to me but the error is:
SQL 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 'WHERE type!="raw")) as time, MAX(t.filler) as filler,m.anime_name, m.furl, m.vid' at line 1
EDIT: Correct answer, but I had to add some grouping as it was returning more than one result. Thank you!
You have forgot to add the from table statement
Try this::
SELECT
t.episode,
t.anime_id,
MAX(t.type) as type,
MIN(t.HD) as HD,
MAX(t.thumbnail) as thumbnail,
IF((type="raw"),(MIN(t.saved)), (SELECT MIN(saved) from anime_episodes WHERE type!="raw")) as time,
// UP HERE ^
MAX(t.filler) as filler,
m.anime_name,
m.furl,
m.video_thumb
FROM anime_episodes t
LEFT JOIN anime_list m ON
( t.anime_id=m.anime_id )
WHERE t.approved=1 AND t.locked=0 AND m.status=0 AND t.episode>=m.latest_ep
GROUP BY t.anime_id, t.episode
ORDER BY time DESC LIMIT 0,24

Adding a date format to FROM_UNIXTIME breaks query

Trying to run this query:
SELECT FROM_UNIXTIME(offers_consumers_history.date,
`'%d-%m-%Y')` AS date,
COUNT(*) AS COUNT
FROM (`offers_history`)
JOIN `offers_consumers_history` ON `offers_consumers_history`.`offer_history_id`=`offers_history`.`id`
WHERE `offers_history`.`merchant_id` = 1
AND `offers_history`.`offer_type_id` = 1
GROUP BY DATE(FROM_UNIXTIME(offers_consumers_history.date))
ORDER BY `offers_consumers_history`.`date`
If I run it without a date format on my first FROM_UNIXTIME (the %d-%m-%Y part), everything runs fine, but I'm obviously not getting the right date format displayed, the returned error 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 'FROM (`offers_history`) JOIN `offers_consumers_history` ON `offers_consumers_his' at line 2
Without a date format I'm getting results such as:
{"date":"2010-08-18 18:40:00","count":"2"}
What I want is:
{"date":"18-08-2010","count":"2"}
count is a reserved word, you can't use it as alias, use different name
'%d-%m-%Y') AS dt,
COUNT(*) AS cnt
OR with backticks
(offers_consumers_history.date,'%d-%m-%Y') AS `date`,
COUNT(*) AS `COUNT`
Remove the () from the table name and change the alias name of count:
SELECT FROM_UNIXTIME(offers_consumers_history.date,
'%d-%m-%Y') AS `date`,
COUNT(*) AS cnt
FROM `offers_history`
JOIN `offers_consumers_history` ON `offers_consumers_history`.`offer_history_id`=`offers_history`.`id`
WHERE `offers_history`.`merchant_id` = 1
AND `offers_history`.`offer_type_id` = 1
GROUP BY DATE(FROM_UNIXTIME(offers_consumers_history.date))
ORDER BY `offers_consumers_history`.`date`

error in my mysql query

i have a query that fetches data's from differant tables
SELECT p.USER_NAME,count(*) as total, pi.UpdatedDate FROM purchase p JOIN purchasedissues pi on pi.PurchaseId=p.PURCHASE_ID WHERE pi.UpdatedDate>DATE_SUB(NOW(), INTERVAL 1 HOUR) AND p.PURCHASE_DATE=CURDATE() AND p.USER_NAME NOT IN (SELECT username from tbl_test_user) GROUP BY p.USER_NAME having count(*)>2
but i got an error that
#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 'SELECT p.USER_NAME,count(*) as total, pi.UpdatedDate FROM
purchase p JOIN pur' at line 1
i can't figure it. please any one help me
thanks in advance
Your query is ok. The error is indicating that it is near your SELECT p.USER_NAME,.... This usually happens when you are executing multiple query without terminating it. Ex,
SELECT * FROM tableName
SELECT p.USER_NAME,count(*) as total, pi.UpdatedDate FROM purchase....
so to correct it, just add a delimiter (usually if not change, it's semi-colon) to end your first query
SELECT * FROM tableName; -- <== this one
SELECT p.USER_NAME,count(*) as total, pi.UpdatedDate FROM purchase....
Try this query! not sure if it works as I don't have the environment set in my machine but it may give you an essence to test it :)
SELECT p.USER_NAME,count(*) as total FROM purchase p JOIN purchasedissues pi on pi.PurchaseId=p.PURCHASE_ID WHERE pi.UpdatedDate>DATE_SUB(NOW(), INTERVAL 1 HOUR) AND p.PURCHASE_DATE=CURDATE() AND p.USER_NAME NOT IN (SELECT username from tbl_test_user) GROUP BY p.USER_NAME having total > 2

MySQL query from subquery not working

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.