开发者

mysql - php blob field?

I have a query that returns as a result what seems to be a 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 

the table is

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;

The main idea is that i want a string per row returning value:::count separated by ###.

The problem is that php returns empty result. I have tried both mysql and mysqli as far as drivers is concerned. Any ideas?

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);
    }

Even if you execute it through phpmyadmin it returns null. Try it on EMS mysql it works like a charm.

full query

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_i开发者_如何学Gotems 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


You are querying for fields matching the session variable @fieldid but I would guess you're not defining that variable in your mysqli session or in your phpmyadmin environment.

SET @fieldid := 1234;
SELECT ... WHERE fieldid = @fieldid ...

Or else you can use ? as a query parameter placeholder for the fieldid and bind it to a PHP variable.

See http://php.net/manual/en/mysqli-stmt.bind-param.php


Does the following query do what you want, without using the @fieldid session variable?

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜