简体   繁体   English

SQLite-在同一表中联接行

[英]SQLite - Joining Rows in the same Table

I have a table with the following columns 我有一个包含以下各列的表格

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.

If at the time someone orders two or three items at the same time two or three rows are entered into the table, but PersonID, RecordDate and OperatorID would be the same. 如果当时有人同时订购两个或三个项目,则在表中输入了两行或三行,但PersonID,RecordDate和OperatorID相同。 Someone can order unto 8 items at the same time. 有人可以同时订购多达8件商品。

Now i have a structure with the following layout. 现在我有一个具有以下布局的结构。

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

How can i construct a sql query so that it combines the rows in the table if PersonID and RecordDate and OperatorID are the same, but gives me the full list of OrderedID details. 如果PersonID和RecordDate和OperatorID相同,我该如何构造一个sql查询,以便合并表中的行,但可以提供OrderedID详细信息的完整列表。

If i have table 'TABLE' with rows 'VALUE', 'CATEGORY' i use: 如果我的表“ 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'

So you have to use something similar to 所以你必须使用类似

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 ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM