delimited mysql_在MySQL中存儲逗號分隔的數據
I've read that this is a no-no, but I'm wondering if this is ALWAYS the case. I've got a list of "dining tables" that can accommodate an array of seats.
我已經讀到這是一個禁忌,但我想知道這是否總是如此。我有一張可以容納一系列座位的“餐桌”清單。
You can see in the image that table_num 12 will accommodate 2 OR 3 seats.
您可以在圖像中看到table_num 12將容納2 OR 3個席位。
So, now my current solution is to query all the table records (according to the user_index for undisclosed reasons) then loop through the results looking for tables that can fit 3 people (if it happens to be three people looking for a table).
所以,現在我現在的解決方案是查詢所有表記錄(根據user_index出於未公開的原因),然后遍歷結果,查找可以容納3人的表(如果恰好有三個人正在尋找表)。
I do that by using the array_implode() method (in php) on the 'seats' column of my return data. I feel like this is an easier solution than creating a separate 'seats' table and then assigning a table_index and user_index to each seating possibility, and then having to run a second query to find the table_num in the original 'tables' table.
我通過在返回數據的“席位”列上使用array_implode()方法(在php中)來做到這一點。我覺得這比創建一個單獨的“席位”表然后為每個座位可能性分配table_index和user_index更容易,然后必須運行第二個查詢以在原始“表”表中找到table_num。
Basically, by using array_implode() I am able to circumvent a second query. But, I don't know if that is less taxing. Or, possibly I am missing out on some awesome query language (like relational table lingo?)
基本上,通過使用array_implode(),我可以繞過第二個查詢。但是,我不知道這是否減稅。或者,我可能錯過了一些很棒的查詢語言(比如關系表術語?)
3 個解決方案
#1
2
If you removed the seats column from this table and then had a new seats table with three columns: id, table_index, and num_seats, where table_index relates to index in your existing table (many-to-one as there may be multiple entires per table in seats). The you could select the tables you want like so:
如果您從此表中刪除了席位列,然后有一個包含三列的新席位表:id,table_index和num_seats,其中table_index與現有表中的索引相關(多對一,因為每個表可能有多個entires在座位上)。您可以選擇所需的表格,如下所示:
SELECT tables.* FROM tables
INNER JOIN seats ON tables.index = seats.table_index
WHERE seats.num_seats = ?
#2
4
Create a table that's called something like TableSeats. In it, you'd have a table_num and a seats column. Use the table_num column as a foreign key. Then, to find tables that seat 3 people, it'd be like so:
創建一個名為TableSeats的表。在其中,你有一個table_num和一個席位列。使用table_num列作為外鍵。然后,為了找到可容納3個人的桌子,它會是這樣的:
select
*
from
tables t
where
exists (select 1 from tableseats s where s.seats = 3)
The magic here is the exists clause. Certainly, you could also do an inner join, but I'd recommend exists here, because this allows you to find any tables that can seat at least 7 people by saying where s.seats >= 7, or even a table that can seat between 5 and 8 people with where s.seats between 5 and 8.
這里的魔力是exists子句。當然,你也可以做一個內連接,但我建議在這里存在,因為這可以讓你找到任何可以容納至少7個人的桌子,說s.seats> = 7,甚至可以坐的桌子5到8人之間,其中s.seats在5到8之間。
#3
4
Correct me if I am wrong, but this does not sound a list. It is more like a range (like, from 2 up to 3 seats). So you could have a better structure by replacing the seats field by the fields min_seats and max_seats. The queries are straight forward then.
如果我錯了,請糾正我,但這不是一個清單。它更像是一個范圍(例如,從2到3個座位)。因此,您可以通過字段min_seats和max_seats替換seat字段來獲得更好的結構。那么查詢是直截了當的。
總結
以上是生活随笔為你收集整理的delimited mysql_在MySQL中存儲逗號分隔的數據的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女首富杨惠妍接任碧桂园董事会主席:公告年
- 下一篇: 《王者荣耀》干将莫邪画中仙皮肤公布:中国