I want current week data from mysql but problem is my field is varchar how i get the data
Error Number: 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 'YEARWEEK(CURDATE()) IS NULL' at line 3
SELECT `E`.`event_id`
FROM (`tbl_events` as E)
WHERE YEARWEEK(date_format(str_to_date(E.event_end,'%m-%d-%Y'),'%Y-%m-%d')),YEARWEEK(CURDATE()) IS NULL
You just have an extra ) before the 2nd yearweek() call, which should go at the end of the expression. Syntax wise.
However, I think you are overcomplicating this a bit:
SELECT `E`.`event_id`
FROM (`tbl_events` as E)
WHERE YEARWEEK(str_to_date(E.event_end,'%m-%d-%Y'))=yearweek(curdate())
On the left hand side of the expression I get the week of the year from the record. This should equal to the week of the year for the current week, which is on the right hand side of the expression.
Related
I am executing this query in MySql:
SELECT amount
FROM Prices
WHERE (item_id = 1246 AND
('2016-12-26' BETWEEN (effective_date AND COALESCE(end_date, NOW()))))
But for some reason I get a syntax error that I don't see where it is.
the 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 ')) AND(item_id = 1419 AND ('2017-01-14' BETWEEN (effective_date AND COALESCE(end' at line 1
the Price is like this:
Prices
id
item_id
effective_date
end_date
I don't think there should be parentheses between BETWEEN and the first term of that expression. Something like this should work:
SELECT amount
FROM Prices
WHERE item_id = 1246 AND
'2016-12-26' BETWEEN effective_date AND COALESCE(end_date, NOW())
This question is a typo, but maybe this answer would be useful to anyone who wants to know the proper way to use BETWEEN.
The MySQL documentation for BETWEEN doesn't explicitly mention anything about parentheses, but it seems to be implying this based on the examples given.
Based on testing this locally, parentheses around each of the two terms in the BETWEEN expression are OK, e.g.
WHERE '2016-12-26' BETWEEN (effective_date) AND (COALESCE(end_date, NOW()))
However, putting parenthesis around the entire clause generates an error, which is what you were doing:
WHERE '2016-12-26' BETWEEN (effective_date AND COALESCE(end_date, NOW()))
I am trying to run a sub query within my TIMESTAMP function in MySQL v5.7.11.
SELECT TIMESTAMP(SELECT NOW() AS currentDate);
I get an error saying
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 NOW() AS currentDate)' at line 1
What am I doing wrong here?
You need to add extra pair of brackets inside your subquery.
SELECT TIMESTAMP((SELECT NOW() AS currentDate));
SELECT DAY_ADD(DATE(scp.create_date),INTERVAL scp.subscription_period_days DAYS)
FROM `subscription` scp;
I am getting error as :
#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 'DAYS) FROM subscription scp
what would be solution if I am adding days using some column values. should I use some other mysql function. Please rectify.
Try this one:
SELECT DAY_ADD(scp.create_date,INTERVAL scp.subscription_period_days DAY)
FROM `subscription` scp;
INTERVAL DAY instead of DAYS should work better.
This question is all about laziness... I'd like to do something like this:
select some_func(some_col), * from my_table
So that I don't have to do this:
select some_func(some_col), col_1, col_2... col_ad_infinitum from my_table
Is there any way to make the first query work? This is the error I get when I run it:
ERROR 1064 (42000) at line 1: 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 my_table' at line 1
Do you mean that in MySQL your first query:
SELECT some_func(some_col), *
FROM my_table
produces this error?:
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 '*' at line 1
You can change your code into (this results in no errors!):
SELECT *, some_func(some_col)
FROM my_table
or into this, if you want to have the calculated columns first:
SELECT some_func(some_col), t.*
FROM my_table AS t
Unfortunately, mysql only supports the asterisk at the start of the column list (unlike every other DB I am familiar with)
(Edited: start not end - oops!)
Change the order of your select params:
select *,some_func(some_col) from my_table
Anyway, as the Zen of Python says: "Explicit is better than implicit". Always try to write the fields you're selecting, and if it's posible try to put the table they're from too, you can use an alias. Your future YOU will thank you.
select t.some_col from my_table t
When I do that with PostgreSQL, I get the column(s) I specify followed by all the other columns (possibly repeating the column(s) I specified).
I need to return average for each of 12 columns I have in a table in DB. MySQL allows one to get average for one column only. The following query (for one column) works:
SELECT station_id, AVG(jan) AS avg_jan
FROM `climate_data`
WHERE element_name = "Temp_mean_mly" AND jan <> -999999
GROUP BY station_id
and the following (for multiple columns) does not (I get syntax error):
SELECT station_id, AVG(jan) AS avg_jan, AVG(feb) AS avg_feb, ... ,
AVG(dec) AS avg_dec
FROM `climate_data`
WHERE element_name = "Temp_mean_mly"
AND jan <> -999999
AND feb <> -999999
AND ...
AND dec <> -999999
GROUP BY station_id
Do I have to use 12 sub-queries to achieve the result I need?
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 'dec)
dec is a reserved MySQL keyword; changing it to `dec` in your query would probably fix that error for you :).
Edit: note that you're also using it in the WHERE clause; it might work there (as it's unlogical for MySQL to find a keyword there), but keep it in mind that you might also have to escape that one :)