error in my mysql query - mysql

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

Related

MySQL, WHERE to contain separate table

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

How to do two different count in one sql

How can do two different count in one table?
The two different count functions count different columns.
Simplified Table:
id,creatorId,resolverId
'1','1','2'
'2','1','2'
'3','2','2'
'4','2','1'
What I want to do is putting the creatorId,COUNT(creatorId),resolverId,COUNT(resolverId) into one table. Like:
creatorId,COUNT(creatorId),resolverId,COUNT(resolverId)
'1','2','1','1'
'2','2','2','3'
I only passed the test of putting them in 2 columns by using UNION, and I tried JOIN but it is illegal to MySQL.
SELECT creatorId, COUNT(creatorId)
FROM issue AS a
GROUP BY creatorId
join(
SELECT resolverId, COUNT(resolverId)
FROM issue AS b
GROUP BY resolverId)
WHERE a.creatorId = b.resolverId;
The error info is:
Error Code: 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 'join( SELECT resolverId, COUNT(resolverId) FROM issue AS b GROUP BY resolverId)' at line 4 0.00034 sec
Can anyone tell me how to deal with it? or give me a example?
Thank you!
select a.creatorId,COUNT(a.creatorId), t.resolverId, count_resolved_id
from issue a
inner join (
SELECT b.resolverId, COUNT(b.resolverId) count_resolved_id
FROM issue AS b
GROUP BY resolverId
) t on t.resolverId = a.creatorId
group by a.creatorId;
you could jon this way
select a.creatorId,COUNT(acreatorId), resolverId, count_resolved_id
from issue a
inner join (
SELECT resolverId, COUNT(resolverId) count_resolved_id
FROM issue AS b
GROUP BY resolverId
) t on t.resolverId = a.creatorId
group by a.creatorId,COUNT(acreatorId)
If I understand correctly, then one way of doing this is an aggregation after a union all:
select id, sum(creator) as creator_cnt, sum(resolver) as resolver_cnt
from ((select creator_id as id, 1 as creator, 0 as resolver
from issue
) union all
(select resolver_id as id, 0 as creator, 1 as resolver
from issue
)
) cr
group by id;

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`

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.