简体   繁体   English

MySQL-如何从不同的表排序行?

[英]MySQL - How to order rows from different tables?

Imagine I have a database with four tables: 'shopping_list', 'sweets', 'books', 'clothes' 想象一下,我有一个包含四个表的数据库:“ shopping_list”,“ sweets”,“ books”,“ clothes”

A user can add any number of any item(s) to their shopping list and rearrange the order before submitting it. 用户可以在购物清单中添加任意数量的任何商品,并在提交之前重新排列订单。 I'm trying to work out the best way of storing that information in the order they submitted it so I can easily/efficiently display it on screen later. 我正在尝试找出按提交顺序存储该信息的最佳方法,以便稍后可以轻松/有效地在屏幕上显示该信息。

Currently the item tables (sweets, books, clothes) store the shopping_list ID and an order position which is incremented as I loop through each shopping_list item and insert it into the relevant table. 当前,项目表(甜品,书籍,衣服)存储shopping_list ID和一个订单位置,当我循环浏览每个shopping_list项目并将其插入相关表时,该订单位置将递增。

The item tables have completely different structures with item specific fields. 物料表具有与物料特定字段完全不同的结构。

So I might end up with something like: 因此,我可能会得到类似以下内容的结果:

shopping_list 购物清单

ID: 1  UserId:123

sweets 甜食

ID: 1  ListId: 1  Order: 1  Flavour: 'Strawberry'  Colour: 'Red'
ID: 2  ListId: 1  Order: 4  Flavour: 'Cola'        Colour: 'Brown'

books 图书

ID: 1  ListId: 1  Order: 3  Name: 'MySQL for Dummies' Author: 'Joe Bloggs'

clothes 衣服

ID: 1  ListId: 1  Order: 2  Type: 'Hat'  Size: 'Large'

The only way I have found to SELECT this data in the correct order so far is to UNION three SELECTS from the item tables and ORDER BY the common 'Order' field, but with the unique fields in each table (15-20 per table), I would have to SELECT a lot of NULL fields each time to match the number of columns. 到目前为止,我发现以正确的顺序选择此数据的唯一方法是从项目表中联合三个SELECTS并按公共“ Order”字段进行ORDER BY,但每个表中都有唯一的字段(每个表15-20) ,我每次必须选择很多NULL字段以匹配列数。

ie

SELECT Id, Order, Flavour, Colour, NULL, NULL, NULL, NULL FROM sweets WHERE ListId=1
UNION
SELECT Id, Order, NULL, NULL, Name, Author, NULL, NULL FROM books WHERE ListId=1
UNION
SELECT Id, Order, NULL, NULL, NULL, NULL, Type, Size FROM clothes WHERE ListId=1
ORDER BY Order

This is obviously a smaller example.... To make this work on my actual tables, I have to write NULL about 20 times on each select. 显然,这是一个较小的示例。...为了使它在我的实际表上起作用,我必须在每个选择上写入NULL约20次。 Am I going about this completely the wrong way? 我会完全以错误的方式去做吗?

Thanks in advance for any help. 在此先感谢您的帮助。

You're probably going about it wrong, yes. 您可能正在做错事情,是的。 You should have an extra table which contains an item's place on the shopping list and a field that represents where the extra information for that field is stored. 您应该有一个额外的表格,其中包含商品在购物清单上的位置,以及一个字段,该字段表示该字段的额外信息的存储位置。 That will let you query things in order and then select the other information (if you need it) from the other tables. 这将使您可以按顺序查询事物,然后从其他表中选择其他信息(如果需要)。

Right now you've scattered information that belongs together in 3 different tables, which is why you have problems sorting it properly. 现在,您已经将信息分散在3个不同的表中,这就是为什么在正确排序信息时遇到问题的原因。

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

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