-1

我有一个这样的数据库结构:

Insured

+-----------+--------------+-----+
| InsuredID |         Name | ... |
+-----------+--------------+-----+
|         1 |     John Doe | ... |
|         2 |   Bill Gates | ... |
|         3 |   Steve Jobs | ... |
+-----------+--------------+-----+

Accompany

+-------------+-----------+---------------+-----+
| AccompanyID | InsuredID |          Name | ... |
+-------------+-----------+---------------+-----+
|           1 |         1 |    Joanna Doe | ... |
|           2 |         1 |    Johnny Doe | ... |
|           3 |         3 | Steve Ballmer | ... |
+-------------+-----------+---------------+-----+

我怎样才能得到这个结果:

+----------------+-----+
|           Name | ... |
+----------------+-----+
|       John Doe | ... |
|     Joanna Doe | ... |
|     Johnny Doe | ... |
|     Bill Gates | ... |
|     Steve Jobs | ... |
|  Steve Ballmer | ... |
+----------------+-----+

我怎样才能使查询的顺序为 -thenInsuredAccompany地方Accompany.InsuredID相当于Insured.InsuredID之前的Accompany. 应该这样排序:

  1. John Doe(被保险人)InsuredID = 1
  2. 乔安娜·多伊(伴奏)InsuredID = 1
  3. 约翰尼·多伊(伴奏)InsuredID = 1
  4. 比尔盖茨(被保险人)InsuredID = 2
  5. 史蒂夫乔布斯(被保险人)InsuredID = 3
  6. 史蒂夫·巴尔默(伴奏)InsuredID = 3
4

3 回答 3

3

像这样的东西应该工作:

select name
from (
 select insuredid, name, 0 ins
 from insured
 union all
 select insuredid, name, 1 ins
 from  accompany) isq
order by insuredid, ins

基本上插入一个假列来识别这些行来自哪个表,然后按 排序insuredid,然后按我们的任意表号。

于 2012-10-24T06:06:34.400 回答
0
SELECT 
    * 
FROM (SELECT NAme,.... FROM Insured
      UNION ALL
      SELECT Name,.... FROM Accompany)
ORDER BY Name
于 2012-10-24T06:05:43.857 回答
0

未经测试,像这样:

SELECT Name 
FROM (
      SELECT InsuredID, Name FROM Insured 
      UNION 
      SELECT InsuredID, Name FROM Accompany
     ) AS t
ORDER BY InsuredID
于 2012-10-24T06:11:27.207 回答