Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql-如何使用am/pm对日期格式进行排序_Mysql_Sql - Fatal编程技术网

Mysql-如何使用am/pm对日期格式进行排序

Mysql-如何使用am/pm对日期格式进行排序,mysql,sql,Mysql,Sql,请检查查询 SELECT DATE_FORMAT( CAST( CONCAT( table.date, " ", table.time ) ) , '%p' ) FROM table AS table Error: [hash]1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to

请检查查询

SELECT DATE_FORMAT( CAST( CONCAT( table.date,  " ", table.time ) ) ,  '%p' ) 
FROM table AS table

Error:
[hash]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 '), '%p') FROM exp_visitorsday as vd LIMIT 0, 30' at line 2
我在以下情况下尝试过这个查询

SELECT DATE_FORMAT( CAST( CONCAT( table.date,  " ", table.time ) as datetimecol ) as datetimecol,  '%p' ) 
FROM table AS table

SELECT DATE_FORMAT( CAST( CONCAT( table.date,  " ", table.time ) as datetimecol ),  '%p' ) 
FROM table AS table
样本数据:

table.date => 01-08-2013
table.time => 10:00 AM
如何进行查询?此查询有什么问题?

您可以:

SELECT * 
FROM (
    SELECT DATE_FORMAT( 
                                    CONCAT( SUBSTRING_INDEX( "01-08-2013", "-", -1), "-",  
                                    SUBSTRING_INDEX(SUBSTRING_INDEX( "01-08-2013", "-", 2), "-", -1), "-", 
                                    SUBSTRING_INDEX( "01-08-2013", "-", 1), " ", 
                                    IF( SUBSTRING_INDEX( "10:00 PM", " ", -1) = "AM", 
                                                SUBSTRING_INDEX( "10:00 AM", " ", 1),  
                                                CONCAT( SUBSTRING_INDEX(SUBSTRING_INDEX( "10:00 AM", " ", 1), ":", 1) + 12, ":", SUBSTRING_INDEX(SUBSTRING_INDEX( "10:00 AM", " ", 1), ":", -1))  )) , '%Y-%m-%d %h:%i') AS datformat ) AS tmp
ORDER BY tmp.datformat;
说明:

SELECT SUBSTRING_INDEX( "01-08-2013", "-", 1); #Day
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( "01-08-2013", "-", 2), "-", -1);#Month
SELECT SUBSTRING_INDEX( "01-08-2013", "-", -1); #Year
你可以:

SELECT * 
FROM (
    SELECT DATE_FORMAT( 
                                    CONCAT( SUBSTRING_INDEX( "01-08-2013", "-", -1), "-",  
                                    SUBSTRING_INDEX(SUBSTRING_INDEX( "01-08-2013", "-", 2), "-", -1), "-", 
                                    SUBSTRING_INDEX( "01-08-2013", "-", 1), " ", 
                                    IF( SUBSTRING_INDEX( "10:00 PM", " ", -1) = "AM", 
                                                SUBSTRING_INDEX( "10:00 AM", " ", 1),  
                                                CONCAT( SUBSTRING_INDEX(SUBSTRING_INDEX( "10:00 AM", " ", 1), ":", 1) + 12, ":", SUBSTRING_INDEX(SUBSTRING_INDEX( "10:00 AM", " ", 1), ":", -1))  )) , '%Y-%m-%d %h:%i') AS datformat ) AS tmp
ORDER BY tmp.datformat;
说明:

SELECT SUBSTRING_INDEX( "01-08-2013", "-", 1); #Day
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX( "01-08-2013", "-", 2), "-", -1);#Month
SELECT SUBSTRING_INDEX( "01-08-2013", "-", -1); #Year

CAST(CONCAT(table.date,”,table.time))
。。。投什么?谢谢约阿希姆。投入一段时间。我问错了。我将更改并检查它。
CAST(CONCAT(table.date,”,table.time))
。。。投什么?谢谢约阿希姆。投入一段时间。我问错了。我去换一下看看。谢谢GaCon。我已经用这个查询来做了。但我失败了。你能检查一下吗?谢谢GaCon。我很好。我有一个无效的数据库数据。完美的干得好..去吧!!谢谢GaCon。我已经用这个查询来做了。但我失败了。你能检查一下吗?谢谢GaCon。我很好。我有一个无效的数据库数据。完美的干得好..去吧!!