简体   繁体   English

如何检查一个记录中的一组值中是否存在一个值

[英]How to check whether a value exists among a set of values in a record in mysql

I am using MySQL in such a way that the user enters a date and flights appear on that day.I was too lazy to put many dates so what I did was made certain flights run on certain days. 我使用MySQL的方式是用户输入日期并在当天显示航班。我懒得输入很多日期,所以我所做的是使某些航班在某些日子运行。

The table planes contains many columns out of which dtd(column containing many days of type varchar(50)) . 表平面包含许多列,其中dtd(列包含许多天,类型为varchar(50))。

One record(in dtd) is Monday, Tuesday The user enters the date 2018-12-03 which is a Monday. 一条记录(在dtd中)是星期一,星期二用户输入日期为2018-12-03的星期一。

My problem is how do I extract only the record which has Monday?(like the monday,Tuesday one)?? 我的问题是如何仅提取星期一的记录?(例如星期一,星期二)?

I tried select.....where instr(day(2018-12-03),dtd)<>0 but I realised that instr gives 0 in 2 cases 1. the character(s) is not present in the record or 2.occurence is at the beginning. 我尝试选择..... where instr(day(2018-12-03),dtd)<> 0但我意识到instr在2种情况下给出0 1.字符不在记录中或2 .occurence是开始。

Like instr(h,hello) and instr(a,hello) gives 0. 像instr(h,hello)和instr(a,hello)给出0。

Please help 请帮忙

I suggest that you use the LIKE operator. 我建议您使用LIKE运算符。
For more info about it please check this link: SQL Like Operator 有关它的更多信息,请检查此链接: SQL Like运算符
To solve your problem, you can try the following: 要解决您的问题,您可以尝试以下操作:

SELECT ...
  WHERE dtd LIKE CONCAT('%', day(2018-12-03), '%');

In this case, you will get all the rows of the table, where the value of the field dtd contains the value of day(2018-12-03). 在这种情况下,您将获得表的所有行,其中字段dtd的值包含day(2018-12-03)的值。
Hope this helps! 希望这可以帮助!

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

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