我有一个简单的查询,我想为每个联盟设置一个组。
SELECT
model,
model_id
FROM
erk
WHERE
5000 between `min` and `max`
UNION ALL
SELECT
model,
model_id
FROM
erk2
WHERE
15000 between `min` and `max`
结果如下:
model_id model
1 20
2 40
3 60
6 150
7 200
8 250
9 300
所以,我想显示每个值的结果(5000和15000) 例如,首先使用值5000选择带来模型20,40,60。第二个选择带来150,200,250,300
我是否应该使用下面的sql设置额外的列,并确认从哪个选择中提取了哪些结果?
model_id model group
1 20 1
2 40 1
3 60 1
6 150 2
7 200 2
8 250 2
9 300 2
设置组ID我能够知道哪一行来自哪个选择..或者可能还有另一种方法可以识别它?
答案 0 :(得分:2)
您只需将组添加为具有常量值的字段:
SELECT
model,
model_id,
1 as result_group
FROM
erk
WHERE
5000 between `min` and `max`
UNION ALL
SELECT
model,
model_id,
2 as result_group
FROM
erk2
WHERE
15000 between `min` and `max`
答案 1 :(得分:1)
使用此
SELECT
model,
model_id,
1 AS `group`
FROM
erk
WHERE
5000 between `min` and `max`
UNION ALL
SELECT
model,
model_id,
2 AS `group`
FROM
erk2
WHERE
15000 between `min` and `max`