订购后如何使用带有单列的联合来显示所有行数据

时间:2014-01-28 08:29:56

标签: mysql sql sql-order-by union

这可能很简单,但我的尝试却一个接一个地失败。

我创建了一个工会,它使用多个表根据“位置”列对数据进行排序。

这是我正在运行的代码:

  SELECT thickness, colto_section, Position
FROM (

SELECT thickness, colto_section,base_layers.position AS Position
FROM base_layers
WHERE base_layers.section_id =1
UNION 
SELECT thickness, colto_section,selects.position
FROM selects
WHERE selects.section_id =1
UNION 
SELECT thickness, colto_section,subbases.position
FROM subbases
WHERE subbases.section_id =1
)x
ORDER BY Position ASC 

现在我的问题是,当我运行此查询时,它只显示最后的列位置,而行上没有数据。我希望它在订购完成后最终显示完整的行数据。希望这是可能的..它会拯救我的屁股。感谢

编辑:MySQL中的输出:

Position
1
2
3

我希望输出:

name  lastname  id  position  
bob   hendric   3      1
den   dyno      5      2
ben   brik      3      3

希望有所帮助

编辑:

谢谢!一切都在100%

2 个答案:

答案 0 :(得分:0)

SELECT name,lastname,id,Position
FROM (

SELECT name,lastname,id,base_layers.position AS Position
FROM base_layers
WHERE base_layers.section_id =1
UNION 
SELECT name,lastname,id,selects.position
FROM selects
WHERE selects.section_id =1
UNION 
SELECT name,lastname,id,subbases.position
FROM subbases
WHERE subbases.section_id =1
)x
ORDER BY Position ASC 

答案 1 :(得分:0)

SELECT name,lastname,Position,section_id  as 'id'
FROM (

SELECT name,lastname,section_id ,base_layers.position AS Position
FROM base_layers
WHERE base_layers.section_id =1
UNION 
SELECT name,lastname,section_id ,selects.position  AS Position
FROM selects
WHERE selects.section_id =1
UNION 
SELECT name,lastname,section_id ,subbases.position  AS Position
FROM subbases
WHERE subbases.section_id =1
)x
ORDER BY Position ASC 

提供名称,姓氏在三个表中。