简体   繁体   English

MySQL SELECT两个值之间的行

[英]MySQL SELECT rows between two values

I need to select data from a mysql database from the past 12 months based on the current date. 我需要根据当前日期从过去12个月的mysql数据库中选择数据。 I have the dates saved in the database as unix timestamps but my query does not seem to work. 我将日期保存为unix时间戳记在数据库中,但查询似乎不起作用。

$query="SELECT user_id, COUNT(first_name) AS member_count
FROM main_user
WHERE renew_date<'$time' AND renew_date>'$old_time' WHERE renew_date!=''";

Basically I need to count all instances of first_name where there is a renew_date timestamp. 基本上,我需要计算有renew_date时间戳的first_name的所有实例。

You have an additional WHERE where you should use AND : 你有一个额外的WHERE ,你应该使用AND

$query="SELECT user_id, COUNT(first_name) AS member_count
FROM main_user
WHERE renew_date<'$time' AND renew_date>'$old_time' AND renew_date!=''";
                                                    ^^^

您的查询中有错误,您有两个WHERE子句!

You put WHERE twice . 您将WHERE放置了两次 You can use From_UNIXTIME function in mysql 您可以在MySQL中使用From_UNIXTIME函数

WHERE FROM_UNIXTIME(renew_date)<NOW() 
    AND FROM_UNIXTIME(renew_date)> (NOW()-INTERVAL 1 year)
    AND renew_date !=''

You can find this and other errors, when you test the return value from your query 测试查询的返回值时,您会发现此错误和其他错误

$query = 'select ...';
$result = $mysqli->query($query);
if ($result === false) {
    // error handling
    echo $mysqli->error;
} else {
    // query successful
    // process result set
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM