Adding a date format to FROM_UNIXTIME breaks query - mysql

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`

Related

How to resolve wrong syntax near HAVING COUNT DISTINCT?

This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use
near 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.

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

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

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.