mysql查询顺序和连接串联字符串

时间:2013-01-01 20:09:48

标签: mysql union

我有这个问题:

SELECT 
  CONCAT(users.id, '#_#', users.nickname, '#_#', users.avatar, '#_#',
         questions.datetime,'#_#',questions.question,'#_#',0000) as summary
FROM
  `questions` join users
  on users.id = questions.id_user 
WHERE
  questions.id = 12 
UNION 
SELECT
  CONCAT(users.id, '#_#', users.nickname, '#_#', users.avatar, '#_#',
         answers.datetime,'#_#',answers.answer,'#_#',answers.id) as summary 
FROM
  answers join users
  on users.id = answers.id_user where id_question = 12

此查询必须以一列(带有连锁数据)结果,我需要通过answers.datetime排序,但我自己无法找到解决方案......

有些mysql大师可以帮帮我吗?

谢谢:)

1 个答案:

答案 0 :(得分:0)

SELECT * FROM (
  SELECT datetime,
    CONCAT(users.id,'#_#',users.nickname,'#_#',users.avatar,'#_#',questions.datetime,'#_#',questions.question,'#_#',0000) as summary 
  FROM `questions` 
  join users on users.id = questions.id_user 
  WHERE questions.id = 12 
  union 
  select datetime,
    CONCAT(users.id,'#_#',users.nickname,'#_#',users.avatar,'#_#',answers.datetime,'#_#',answers.answer,'#_#',answers.id) as summary 
  from answers 
  join users on users.id = answers.id_user where id_question = 12
) A ORDER BY datetime

假设您打算按datetime排序,而不是answers.datetime