我正在运行以下代码,但它会返回错误:"undefined index: value"
$two_tier= mysql_query("
SELECT Count(1)
FROM(
SELECT `login_id`
FROM `data`
WHERE Year(`start_at`) = Year(Date_sub(Now(), INTERVAL 1 month))
AND Month(`start_at`) = Month(Date_sub(Now(), INTERVAL 1 month))
AND end_at > Date_add(start_at, INTERVAL 5 minute)
GROUP BY `login_id`
HAVING Count(`login_id`) > 1
) AS Value
");
$two_cnt = mysql_fetch_assoc($two_tier);
echo $two_cnt['value'];
我想获得“价值”。请帮忙。
答案 0 :(得分:5)
SELECT Count(1) as my_value
FROM (SELECT `login_id`
FROM `data`
WHERE ( Year(`start_at`) = Year(Date_sub(Now(), INTERVAL 1 month))
AND Month(`start_at`) = Month(Date_sub(Now(), INTERVAL 1 month)
) )
AND ( end_at > Date_add(start_at, INTERVAL 5 minute) )
GROUP BY `login_id`
HAVING Count(`login_id`) > 1) AS Value
$two_cnt = mysql_fetch_assoc($two_tier);
echo $two_cnt['my_value'];
您选择FROM as Value,但第一次选择的结果不在该值中,您还需要一个AS
答案 1 :(得分:2)
由于SELECT
语句中最外部的值为SELECT Count(1)
,因此您实际上将Count(1)
取为Count(1)
而不是value
。要将其作为值获取,您需要将其作为:
SELECT Count(1) AS value ....
然后代码将正常工作。所以,最后的陈述将是:
$two_tier= mysql_query("
SELECT Count(1) AS value
FROM(
SELECT `login_id`
FROM `data`
WHERE Year(`start_at`) = Year(Date_sub(Now(), INTERVAL 1 month))
AND Month(`start_at`) = Month(Date_sub(Now(), INTERVAL 1 month))
AND end_at > Date_add(start_at, INTERVAL 5 minute)
GROUP BY `login_id`
HAVING Count(`login_id`) > 1
) AS Value
");