SQLite - 在同一个表中连接行

时间:2014-06-20 11:51:40

标签: sql sqlite

我有一个包含以下列的表

ID - PersonID - OrderedID - PostageID - RecordDate - OperatorID

PersonID - Refers to a table cotaining firstname, secondname etc.
OrderedID - Refers to a table of items they can order.
PostageID -  is an enum.
RecordDate - Is the timedate they made the order.
OperatorID - is the phone operator who made the order.

如果当时有人同时订购了两个或三个项目,则会在表格中输入两到三行,但PersonID,RecordDate和OperatorID将相同。有人可以同时订购8件物品。

现在我有一个具有以下布局的结构。

Order:
First Name
Second Name
OrderedID 1 Name
OrderedID 1 Colour
OrderedID 1 Weight
OrderedID 2 Name
OrderedID 2 Colour
OrderedID 2 Weight
OrderedID 3 Name
OrderedID 3 Colour
OrderedID 3 Weight
OrderedID 4 Name
OrderedID 4 Colour
OrderedID 4 Weight
OrderedID 5 Name
OrderedID 5 Colour
OrderedID 5 Weight
OrderedID 6 Name
OrderedID 6 Colour
OrderedID 6 Weight
OrderedID 7 Name
OrderedID 7 Colour
OrderedID 7 Weight
OrderedID 8 Name
OrderedID 8 Colour
OrderedID 8 Weight
OrderDate
OrderUser

如果PersonID和RecordDate和OperatorID相同,我如何构造一个sql查询,以便它组合表中的行,但是给出了OrderedID详细信息的完整列表。

1 个答案:

答案 0 :(得分:0)

如果我有桌子' TABLE'行' VALUE',' CATEGORY'我用:

SELECT t1.VALUE AS CAT1,
       t2.VALUE AS CAT2,
FROM TABLE AS t1,
     TABLE AS t2
WHERE t1.CATEGORY='cat1'
      t2.CATEGORY='cat2'

所以你必须使用与

类似的东西
SELECT p.f_name,
       p.s_name,
       o1.name,
       o1.color,
       o2.name,
       o2.color
  from Persons as p,
       table as t,
       Orders as o1,
       Orders as o2
 where t.PersonID = p.id
   and o1.id=t.OrderedID
   and o2.id=t.OrderedID
   and ...