订购Concat与Union Concat不合适

时间:2014-10-13 09:04:09

标签: php mysql

我无法根据需要对结果进行排序。

查询:

SELECT b.bid,REPLACE(GROUP_CONCAT(f.val), ',' , '')
FROM boxes as b
JOIN boxgroupstype as g ON (b.bgid = g.bgid AND g.btid =3)
JOIN pages as p ON (p.pid = b.pid AND p.qid = '1')
LEFT JOIN formboxverifychar as f ON (f.bid = b.bid AND f.fid = 66)
WHERE val IS NOT NULL AND vid !=0
GROUP BY varname
UNION
SELECT b.bid,b.label
FROM boxes as b
JOIN boxgroupstype as g ON (b.bgid = g.bgid AND g.btid =1)
JOIN pages as p ON (p.pid = b.pid AND p.qid = '1' or p.qid = '2')
LEFT JOIN formboxverifychar as f ON (f.bid = b.bid AND f.fid = 66)
WHERE val IS NOT NULL AND vid !=0
ORDER BY bid asc

所以排序不是根据BID在g.btid = 3的上限值中出现的。

结果:

  

bid REPLACE(GROUP_CONCAT(f.val),',','')
    1它们没用     5它们很有用     7完全没有     12一点点     15根本没有     19 AYNTHON
    43 HO
    68是
    71 TAJLAIYPNAEG
    95 35
    98是
    100 HINESCE
    123男

应该是

  

19 ANTHONY
     71 PETALING JAYA
    100中文

如果有人可以提供帮助,我会非常感激..

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT b.bid,REPLACE(GROUP_CONCAT(f.val), ',' , '') FROM boxes as b JOIN boxgroupstype as g ON (b.bgid = g.bgid AND g.btid =3) JOIN pages as p ON (p.pid = b.pid AND p.qid = '1') LEFT JOIN formboxverifychar as f ON (f.bid = b.bid AND f.fid = 66) WHERE val IS NOT NULL AND vid !=0 GROUP BY varname UNION SELECT b.bid,b.label FROM boxes as b JOIN boxgroupstype as g ON (b.bgid = g.bgid AND g.btid =1) JOIN pages as p ON (p.pid = b.pid AND p.qid = '1' or p.qid = '2') LEFT JOIN formboxverifychar as f ON (f.bid = b.bid AND f.fid = 66) WHERE val IS NOT NULL AND vid !=0 ORDER BY f.bid asc

答案 1 :(得分:0)

我能够自己解决问题。感谢帮助朋友。

SELECT b.bid,

REPLACE(GROUP_CONCAT(f.val ORDER BY b.bid),',','')

FROM box as b

将boxgroupstype加入为g ON(b.bgid = g.bgid AND g.btid = 3)

将页面加入为p ON(p.pid = b.pid AND p.qid =' 1')

LEFT JOIN formboxverifychar为f ON(f.bid = b.bid AND f.fid = 66)

WHERE val IS NOT NULL且vid!= 0

GROUP BY varname

UNION

SELECT b.bid,b.label

FROM box as b

将boxgroupstype加入为g ON(b.bgid = g.bgid AND g.btid = 1)

将页面加入为p ON(p.pid = b.pid AND p.qid =' 1'或p.qid =' 2')

LEFT JOIN formboxverifychar为f ON(f.bid = b.bid AND f.fid = 66)

WHERE val IS NOT NULL且vid!= 0

ORDER BY bid asc