Trying to select a query in php/mysql to get "Upcoming Items" in a calendar. We store the dates in the DB as a unix time. Here's what my query looks like right now
SELECT *
FROM `calendar`
WHERE (`eventDate` > '$yesterday')
OR (FROM_UNIXTIME(eventDate, '%m') > '$current_month' AND `$yearly` = '1')
ORDER BY `eventDate`
LIMIT 4
This is giving me an error "Unknown column '' in 'where clause'". I'm sure it has to do with my use of parenthesis (which I've never used before in a query) and the FROM_UNIXTIME command.
Can someone help me out and let me know how I've screwed this up?
Thanks!
This to me looks suspicious:
`$yearly`
What is the value of $yearly? Is it empty? When you use backticks MySQL treats the contents as the name of a column.
Perhaps you meant to create a string instead, in which case you should use a different type of quote:
'$yearly' = '1'
Or perhaps you intended to refer to the column yearly:
yearly = '1'
Another tip is to print the SQL query after the PHP variables have been interpolated as this sometimes makes it easier to understand the error message from MySQL. I'd imagine your query looks something like this:
SELECT *
FROM `calendar`
WHERE (`eventDate` > '1295071200')
OR (FROM_UNIXTIME(eventDate, '%m') > '1' AND `` = '1')
ORDER BY `eventDate`
LIMIT 4
The suspicious part is here:
`` = '1'
Do you have a column named $yearly ?, try removing the dollar sign
Related
I've read multiple posts and find nothing wrong with my syntax, can someone point out the error?
I am testing some queries in PHP MyAdmin, on a WordPress Database. The table I am querying has a meta_key of "Listing-End-Date" and meta_values of "2018/06/30".
My query looks like this:
SELECT * FROM `table`
WHERE `meta_key` LIKE 'Listing-End-Date'
AND STR_TO_DATE('meta_value', '%Y/%m/%d') > CURDATE()
and it returns 0 results. To test my STR_TO_DATE format, I did a new query:
SELECT `meta_key` , STR_TO_DATE( 'meta_value', '%Y/%m/%d' )
FROM 'table'
WHERE `meta_key` LIKE 'Listing-End-Date'
I get the expected 1 result returned, but the date is NULL. Is it because I am using PHP My Admin or did I type something wrong?
Meta_value should be ticked and not quoted inside string_to_date
You were trying to convert string value 'meta_value' to date which obviously fails. You probably wanted to reference a column meta_value from table meta_key instead:
SELECT * FROM `table`
WHERE meta_key LIKE 'Listing-End-Date'
AND STR_TO_DATE(meta_value, '%Y/%m/%d') > CURDATE()
I've removed backticks as they're not required here (all but table). Though, you really shouldn't name your table with a reserved keyword even if you can using backticks.
Also, one thing to note would be that meta_key LIKE 'Listening-End-Date' is equivalent of equality comparison meta_key = 'Listening-End-Date' because you don't do partial search (you're not using % anywhere).
Try to use DATE_FORMAT()
check this page
Am generating sql query string from other stuff. I would like to have an alias and ORDER B for every selected item, mainly so that if two select parts come up with the same alias they will be coerced into unique column names in the result by appending -1, -2 etc rather than having multiple columns with the same name.
Simplified query:
SELECT '6' - `Amt`
FROM tbl
produces a result with column name '6' - `Amt` (as expected).
However, adding an alias:
SELECT '6' - `Amt` AS '6' - `Amt`
FROM tbl
produces an error.
Yet the same format in ORDER BY is fine:
SELECT '6' - `Amt`
FROM tbl
ORDER BY '6' - `Amt`
https://dev.mysql.com/doc/refman/5.7/en/problems-with-alias.html does talk about this.
The only thing I can think to do is strip the alias back to 6 - Amt but that is different to '6' - `Amt` which mysql creates if there is no alias.
Any suggestions?
If you really really really want ` in your alias you can "escape" them by doubling them up, like `6 - ``Amnt```
... but I would recommend against that unless there is a very very good reason for it.
SELECT '6' - 'Amt' as ` '6' - ``Amt`` `
I am unsure with what you are trying to achieve, but this is possible by multiplying the ` in your query.
Im not sure why you would do this since this is bad practise, and I would advice not using this unless you must have to.
I am trying to understand a piece of mysql code:
Select * from tableA
where type = 'blue'
and status = 'confirmed'
and statement
and date between '2017-01-01' and '2017-12-31'
Would would the "and statement" mean where statement is a field but without an =, or, and, >, < ect.
Thanks in advance
This is a MySQL peculiarity that other database engines do not exhibit. In other DBMS the equivalent would be:
and statement<>0
An empty where condition, as above, is effectively the same as AND LENGTH(statement) > 0. So any non-empty value in the statement column will be returned.
I have my date in database in varchar column and i can't change it. However i want to sort things from newest to latest. My date in database looks like:
2014-09-22 10:28:28
So what i try is something like:
$sql = "SELECT * FROM axnmrs_cases WHERE vin = :vin ORDER BY STR_TO_DATE(date_created,'%b-%e-%Y') ASC LIMIT 30";
But unfortunately this not change anything for me , even if i change ASC to DESC , nothing changeing in result
and also something like:
$sql = "SELECT * FROM axnmrs_cases WHERE vin = :vin ORDER BY CONVERT(date_created, date, 103)";
This throw syntax SQL error and I have no idea why.
Is here anybody who can show me the right way?
Date stored in varchar is not a real date and hence the order by also does not give you what you want. The best approach would be store date always in mysql native data types. However in your case you can use str_to_date() function to convert the varchar dates to real date and then use it for sort something as
order by str_to_date(date_created,'%Y-%m-%d %H:%i:%s');
$sql = "SELECT * FROM `axnmrs_cases` WHERE `vin` = ':vin' ORDER BY `date_created` ASC LIMIT 30";
Already tried something like this?
You are using the wrong format in your STR_TO_DATE function, if the date is in the format:
2014-09-22 10:28:28
Then you need to use
STR_TO_DATE(date_created, '%Y-%m-%d %H:%i:%s')
i.e. the format you give should match the format your varchar is in.
Example on SQL Fiddle
In your case you are using '%b-%e-%Y', so you are looking for a date like Jan-1-2014, for a full list of the specifiers in the format defintion see the My SQL Docs for DATE_FORMAT
Also, CONVERT(date_created, date, 103) does not work because it is SQL Server Syntax.
Finally, I would really, really try and change the column data type. Storing dates as a varchar is never a good idea. Anything else is just a workaround, not a solution.
I have a Db with one table with 3 fields like the following:
user_id TimeStamp Azioni
where the 'timestamp' field is a varchar(25) like this: 2012/09/19 16:34:01.95
It is a varchar and not a timestamp value because i need it to be in the shown format.
And i cannot change its type even if i wanted to.
Now, I'm trying to get all db entries with the same date. For example, when Timestamp contains 2012/09/19
I tied several queries:
Query 0:
SELECT Azioni.Action
FROM Azioni
WHERE TimeStamp LIKE '2012/09/19%'
Query 1:
SELECT `Azioni`.*
FROM Azioni
Where `TimeStamp` LIKE '{2012/09/19}%'
Query 2:
SELECT `Azioni` . *
FROM Azioni
WHERE LOCATE( '2008/09/19', `TimeStamp` ) >0
Query 3:
SELECT `Azioni` . *
FROM Azioni
WHERE INSTR( `TimeStamp` , '2012/09/19' ) >0
Query 4:
SELECT * FROM `Azioni`
WHERE `TimeStamp` like '2012|/09|/19%' escape '|'
and I always get: MySQL returned an empty result set (i.e. zero rows).
But I am sure there are rows containing the said timestamp. What am i doing wrong? Does the 'space' between date and time create a problem? If so how can i solve it? Do you have any suggestion?
EDIT:
Aa suggested, from
SELECT TIMESTAMP, HEX( TIMESTAMP )
FROM Azioni
i get the following
2009-06-06 09:28:00.0000 323030392D30362D30362030393A32383A30302E30303030
2009-06-06 09:29:00.0000 323030392D30362D30362030393A32393A30302E30303030
2009-06-06 09:30:51.0000 323030392D30362D30362030393A33303A35312E30303030
2009-06-06 14:25:00.0000 323030392D30362D30362031343A32353A30302E30303030
2009-06-06 14:26:00.0000 323030392D30362D30362031343A32363A30302E30303030
EDIT 2:
ehm yeah, i was typing the date wrong in the query. Sigh, i'm stupid. Sorry for wasting your time guys.
How about this:
where timestamp like '2012/09/19%'
And, if you are going to call the field timestamp you should store it as a date/datetime/timestamp. Call it something else if it is going to be stored as a string. Timestamp is actually the name of a type in MySQL, so having that in a column name with a different type is quite misleading.
EDIT:
Have you tried:
where left(timestamp, 10) = '2012/09/19'
It sounds like there are string characters in the field, which are preventing reasonable code from working.
SELECT * FROM Azioni
WHERE `TimeStamp' LIKE '2012/09/19%'