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中存儲逗號分隔的數據相关推荐

  1. checkpoint技术mysql_认识MySQL中的Checkpoint技术

    1,checkpoint产生的背景 数据库在发生增删查改操作的时候,都是先在buffer pool中完成的,为了提高事物操作的效率,buffer pool中修改之后的数据,并没有立即写入到磁盘,这有可 ...

  2. 日期加星期存入mysql_在MySQL中添加两个星期的日期?

    要在MySQL中添加两个星期的日期,请使用DATE_ADD()-insert into yourTableName(yourColumnName) values(date_add(now(),inte ...

  3. buffer pool mysql_理解Mysql中的Buffer pool

    Buffer Pool在数据库里的地位 1.回顾一下Buffer Pool是个什么东西? 数据库中的Buffer Pool是个什么东西?其实他是一个非常关键的组件,数据库中的数据实际上最终都是要存放在 ...

  4. rank()函数 mysql_在MySQL中实现Rank高级排名函数

    在MySQL中实现Rank高级排名函数 MySQL中没有Rank排名函数,当我们需要查询排名时,只能使用MySQL数据库中的基本查询语句来查询普通排名.尽管如此,可不要小瞧基础而简单的查询语句,我们可 ...

  5. avg最多用多少列 mysql_使用MySQL中的AVG函数求平均值的教程

    MySQL的AVG函数是用来求出各种记录中的字段的平均值. 要了解AVG功能考虑EMPLOYEE_TBL表具有以下记录: mysql> SELECT * FROM employee_tbl; + ...

  6. executeupdate mysql_使用Mysql中的executeUpdate在SQL语句中创建表

    我有以下内容doGet(): protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ...

  7. mysql memcache redis_redis,mysql,memcache的區別與比較,redis兩種數據存儲持久化方式

    redis與mysql比較 區別:mysql中一個中小型的網絡數據庫,比oracle和sqlserver小, 但是並發能力遠超過acess這樣的桌面數據庫:redis是一個內存鍵值數據庫,支持網絡.可 ...

  8. mysql存儲過程_Mysql存儲過程 | 學步園

    一.創建存儲過程 在創建存儲過程之前,首先要了解delimiter 分界符,默認sql 語句分界符是 ; 分號,但為了寫較長的存儲過程,需要將默認分割符修改掉,建議修改為 //,如此之後,sql 語句 ...

  9. mdf ldf 转mysql_如何將sql server 的.mdf和.ldf 數據備份文件導入到mysql數據庫中?

    示例 下面的示例將 pubs 中的兩個文件附加到當前服務器. EXEC sp_attach_db @dbname = N'pubs', @filename1 = N'c:\Program Files\ ...

最新文章

  1. oracle递归用法
  2. tomcat server.xml各个端口的作用
  3. day02.3-元组内置方法
  4. java富文本如何转义_富文本编辑器wangEditor中转义字符的问题
  5. Codeforces Round #277 (Div. 2) 题解
  6. 1.4 编程基础之逻辑表达式与条件分支
  7. 营销心理学:如何挣女人的钱?
  8. 滴滴更新招股书:预计定价13.5美元 最多筹集约46亿美元
  9. Python+OpenCV:图像快速角点检测算法(FAST Algorithm for Corner Detection)
  10. yaml和properties文件相互转换的网站
  11. 即时网络通讯系统的设计与实现(QQ)
  12. S32K MCAL02-FlexCAN 时钟模块【理论部分】
  13. 尚硅谷Java入门视频教程(一)编程入门
  14. 金蝶K3 Wise单据导入导出工具使用说明
  15. apk注册机加密_apk软件添加注册机方法 安卓apk添加授权教程
  16. cacti mysql 压缩 备份_cacti数据迁移步骤 CactiEZ重装系统后 恢复原来的数据 CactiEZ重装系统前要备份的数据...
  17. (原)red-green Image.合成三维立体效果, 红绿眼镜 3D图 ,三维图片的核心算法。googler.cc上面有完善的程序和源码下载。 网上以前的那个有错误,我纠正了下~!~
  18. 重庆邮电大学计算机学硕考研经验,重庆邮电大学计算机考研复试备考经验及经过...
  19. 苹果5越狱教程_苹果新越狱工具发布,支持iOS 13最新版,详细安装教程看这里...
  20. SSL网站安全证书https加密防劫持

热门文章

  1. tgp饥荒 服务器无响应,tgp饥荒联机版缺少fmod_event.dll文件怎么办?具体解决方法介绍...
  2. python编写人机交互界面_Python编写个天气查询应用(可视化界面)
  3. 河北省内计算机对口学校有哪些专业,河北省对口单招有哪些高校
  4. python列表元组字符串都属于有序数列_列表、元组、字符串是Python的__________(有序、无序?)序列。...
  5. Spring Boot log4j2 configuration example
  6. python----面对对象三大特征2
  7. 洛谷P3245 [HNOI2016]大数 【莫队】
  8. jQuery 双击事件(dblclick)时,不触发单击事件(click)
  9. Application.mk
  10. ###《Effective STL》--Chapter2