简体   繁体   中英

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. Someone can order unto 8 items at the same time.

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.

If i have table 'TABLE' with rows 'VALUE', 'CATEGORY' i use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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