我有一个查询,返回结果似乎是一个BLOB
SELECT CAST(GROUP_CONCAT(CONCAT(value,'(',qty,')') SEPARATOR ', ') AS CHAR)a
FROM (
SELECT value,count(*) qty
FROM extra_field_values
WHERE fieldid = @fieldid
AND value != ''
AND itemid IN (7,8,10,12,15,16,17,18,19,20,21,22,23,24,25,26)
GROUP BY value
) x
表格是
CREATE TABLE `extra_field_values` (
`itemid` int(11) NOT NULL DEFAULT '0',
`fieldid` int(11) NOT NULL DEFAULT '0',
`value` text NOT NULL,
KEY `itemid` (`itemid`),
KEY `fieldid` (`fieldid`),
KEY `value` (`value`(1)),
KEY `inx` (`itemid`,`fieldid`,`value`(1))
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
主要思想是我希望每行返回一个字符串,以###。
分隔值::: count问题是php返回空结果。就驱动程序而言,我已经尝试过mysql和mysqli。 有什么想法吗?
PHP
$result = $mysqli->query(" SELECT CAST(GROUP_CONCAT(CONCAT(value,'(',qty,')') SEPARATOR ', ') AS CHAR)a
FROM (
SELECT value,count(*) qty
FROM extra_field_values
WHERE fieldid = @fieldid
AND value != ''
AND itemid IN (7,8,10,12,15,16,17,18,19,20,21,22,23,24,25,26)
GROUP BY value
) x ") ;
while( $row = $result->fetch_assoc() ){
print_r($row);
}
即使你通过phpmyadmin执行它,它也会返回null。在EMS mysql上尝试它就像魅力一样。
完整查询
SELECT
extra_field_values.fieldid,
extra_fields.var_name,
extra_fields.field,
extra_fields.type,
@fieldid:=extra_field_values.fieldid,
(
SELECT CAST(GROUP_CONCAT(CONCAT(value,'(',qty,')') SEPARATOR ', ') AS CHAR) a
FROM ( SELECT value, @fieldid, count(*) qty FROM extra_field_values WHERE fieldid = @fieldid GROUP BY value ) x
) as v,
count(itemid) as total,
(SELECT groupid FROM extra_fields_groups_items WHERE itemid = @fieldid) as groupid
FROM extra_field_values INNER JOIN extra_fields
ON (extra_field_values.fieldid=extra_fields.fieldid)
WHERE module = 'listings'
AND itemid IN (7,8,10,12,15,16,17,18,19,20,21,22,23,24,25,26)
AND settings LIKE '%"search";s:1:"1"%'
GROUP BY fieldid
答案 0 :(得分:1)
您正在查询与会话变量@fieldid
匹配的字段,但我猜您没有在mysqli会话或phpmyadmin环境中定义该变量。
SET @fieldid := 1234;
SELECT ... WHERE fieldid = @fieldid ...
否则,您可以使用?
作为fieldid的查询参数占位符,并将其绑定到PHP变量。
请参阅http://php.net/manual/en/mysqli-stmt.bind-param.php
以下查询是否可以执行您想要的操作,而不使用@fieldid会话变量?
SELECT x.fieldid, x.var_name, x.field, x.type, x.groupid, COUNT(*) AS total,
CAST(GROUP_CONCAT(CONCAT(x.value,'(',x.qty,')') SEPARATOR ', ') AS CHAR) a
FROM (
SELECT v.value, v.fieldid, f.var_name, f.field, f.type, g.groupid, COUNT(*) AS qty
FROM extra_field_values AS v
INNER JOIN extra_fields AS f ON f.fieldid=v.fieldid
INNER JOIN extra_fields_groups_item AS g ON g.itemid=v.fieldid
WHERE f.module = 'listings'
AND f.itemid IN (7,8,10,12,15,16,17,18,19,20,21,22,23,24,25,26)
AND f.settings LIKE '%"search";s:1:"1"%'
GROUP BY v.value, v.fieldid
) as x
GROUP BY x.fieldid