日期:2014-05-16  浏览次数:20754 次

如何在sql中查询某个字段不同值分别取一定数量.
SELECT * FROM `表名` WHERE 类型="甲" ORDER BY rand() limit 0,3;
SELECT * FROM `表名` WHERE 类型="乙" ORDER BY rand() limit 0,1;
我想要的就是 取4条记录出来,其中 类型=甲有3条 类型=乙有1条
如何用一条sql语句执行.
望高手给出结果,

------解决方案--------------------
SQL code

select top 3 * from tracy where AZWZ='1330采面'
union all
select top 1* from tracy where AZWZ='223水仓'
/*
AZWZ    PJZ    ZDZ    BCSJ
1330采面    0.33    0.34    2010-01-01 00:00:00.000
1330采面    0.33    0.34    2010-01-01 00:05:00.000
1330采面    0.33    0.34    2010-01-01 00:00:00.000
223水仓    0.33    0.34    2010-01-01 00:00:00.000
*/

感觉不用union all一条语句实现有难度

------解决方案--------------------
探讨

查询不出来阿..报错 #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '3 * FROM `musicData` WHERE musicType=&quo……

------解决方案--------------------
SQL code
(SELECT * FROM `表名` WHERE 类型="甲" ORDER BY rand() limit 0,3)
union all
(SELECT * FROM `表名`  WHERE 类型="乙" ORDER BY rand() limit 0,1);