简体   繁体   English

如何在具有多个值的字段上对MySQL表排序

[英]How to sort MySQL table on a field that has multiple values

I am stuck with a MySQL table created by another developer. 我被另一个开发人员创建的MySQL表所困扰。 Its a holiday cruise table with fields like destinations, ships and sailing dates (among other fields). 它是一个假日游轮表,其中包含目的地,船只和航行日期等字段(以及其他字段)。

Destinations   Ships      Sailing Dates

Antarctica     MS QE2     2 Dec 2018, 1 Jan 2019

Antarctica     MS TBS     2 Feb 2019, 3 Mar 2019

Antarctica     MS JRB     9 Nov 2018, 5 Dec 2018

As indicated, each ship can have multiple sailing dates, entered as comma separated text. 如图所示,每艘船可以有多个航行日期,以逗号分隔的文本输入。 But I am interested only in the first sailing date of each row. 但是我只对每行的第一个航行日期感兴趣。

For example, "2 Dec 2018" is the first date on row one. 例如,“ 2018年12月2日”是第一行的第一个日期。 Likewise, "2 Feb 2019" is the first date on row two, and so on. 同样,“ 2019年2月2日”是第二行的第一个日期,依此类推。

I need a PHP SELECT statement to sort this entire table on the first dates of each row. 我需要一个PHP SELECT语句在每行的第一个日期对整个表进行排序。 To make things simpler, I thought of adding another date field to the table, and copy the first dates of each row onto that new date field, and then sort the table based on that field. 为简化起见,我想到了将另一个日期字段添加到表中,并将每行的第一个日期复制到该新日期字段中,然后根据该字段对表进行排序的方法。 But that is a round-about solution, which I don't prefer. 但这是一种环回解决方案,我不喜欢这种解决方案。

Is there a simple PHP/SQL statement which can achieve this? 是否有一个简单的PHP / SQL语句可以实现这一目标? I thought of the PHP Explode() function. 我想到了PHP Explode()函数。 But I am not sure how I can incorporate that into a SELECT statement. 但是我不确定如何将其合并到SELECT语句中。 Any help would be highly appreciated. 任何帮助将不胜感激。

Update: Thanks for your suggestions. 更新:感谢您的建议。 I totally agree, this table is a mess. 我完全同意,这张桌子是一团糟。 Let's say I move the dates to a new table, with multiple rows for each row in the old table, like the following. 假设我将日期移动到新表中,旧表中的每一行都有多个行,如下所示。

Destinations   Ships      Voyage ID

Antarctica     MS QE2     1

Antarctica     MS TBS     2

Antarctica     MS JRB     3

Dates Table 日期表

--Voyage ID------Sailing Dates -航程编号------航行日期

-------1------------2 Dec 2018 ------- 1 ------------ 2018年12月2日

-------1------------1 Jan 2019 ------- 1 ------------ 2019年1月1日

-------2------------2 Feb 2019 ------- 2 ------------ 2019年2月2日

-------2------------3 Mar 2019 ------- 2 ------------ 2019年3月3日

-------3------------9 Nov 2018 ------- 3 ------------ 2018年11月9日

-------3------------5 Dec 2018 ------- 3 ------------ 2018年12月5日

Now, what SQL command should I use to sort the old table by the first dates of each Voyage ID in the new DATES table. 现在,我应该使用什么SQL命令按新DATES表中每个Voyage ID的开始日期对旧表进行排序。

You are suffering from a horrible database design. 您正遭受着可怕的数据库设计之苦。 Instead of having one table for the ships, one for destinations, and one for actual trips, you have just one table. 您只需要一张桌子,而不是一张桌子就可以坐船,一张桌子就可以到达目的地,一张桌子就可以实际旅行。 This is not what a relational database is supposed to be. 这不是关系数据库应该具有的功能。

Moreover dates are not stored as dates but as strings and there are even multiple dates in one string. 此外,日期不是存储为日期,而是存储为字符串,并且一个字符串甚至包含多个日期。 It can hardly be worse. 几乎不会更糟。 So the best advice is to re-write the whole database. 因此,最好的建议是重新编写整个数据库。

If you don't want to re-design the database or if you are not allowed to, then my advice is: Do not try to tackle this in a SQL query. 如果您不想重新设计数据库或不允许这样做,那么我的建议是:不要尝试在SQL查询中解决这个问题。 Simply read all records from the database instead and then do the string manipulation and sorting in PHP. 只需从数据库中读取所有记录,然后在PHP中进行字符串操作和排序即可。

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

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