PHP和MySQL子查询,其中id为()

时间:2014-05-06 04:56:07

标签: php mysql

我试图运行此查询:

  $sql = "SELECT p.*, c.id as cid, c.name as catname, p.title as ptitle,"
  . "\n CONCAT(u.first,' ',u.last) as user," 
  . "\n (SELECT GROUP_CONCAT(DISTINCT tags) as tagnames FROM tagtable WHERE id IN (p.tagids))" 
  . "\n FROM posts as p" 
  . "\n LEFT JOIN categories as c ON c.id = a.cid" 
  . "\n LEFT JOIN users as u ON u.id = a.uid" 
  . "\n WHERE a.active = 1 LIMIT 1";

我唯一的问题是使用子查询

"SELECT GROUP_CONCAT(DISTINCT tags)..."

由于某种原因,它不会产生任何结果。我希望将标签名称排为

  

" lorem,ipsum,dolores,..."

字段 tag-ids 采用1,2,3格式。

1 个答案:

答案 0 :(得分:2)

如果tagids的格式为'1,2,3',请使用

FIND_IN_SET

  

如果字符串str在,则返回1到N范围内的值   字符串列表strlist由N个子串组成。

所以,像

WHERE FIND_IN_SET(id,p.tagids) > 0

SQL Fiddle DEMO