Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
类似mysql的查询,检查逗号分隔数据中的值_Mysql - Fatal编程技术网

类似mysql的查询,检查逗号分隔数据中的值

类似mysql的查询,检查逗号分隔数据中的值,mysql,Mysql,我有一个这样的数据表 +-------+---------------------+------------------------------------------------+--------------------+ | id | name | movieyears | length(movieyears) | +-------+---------------------+----

我有一个这样的数据表

+-------+---------------------+------------------------------------------------+--------------------+
|  id   |        name         |                   movieyears                   | length(movieyears) |
+-------+---------------------+------------------------------------------------+--------------------+
| 85530 | Nargis Fakhri       | [2011,2013,2014]                               |                 16 |
| 26683 | Nawazuddin Siddiqui | [1999,2006,2007,2009,2010,2012,2013,2014,2015] |                 46 |
| 14508 | Aditi Rao Hydari    | [2009,2011,2012,2013,2014]                     |                 26 |
+-------+---------------------+------------------------------------------------+--------------------+

挑战在于找到参与者仅在过去4年内活跃的行,而不是在任何其他年份活跃的行,因此基本上,一个好的查询应该只返回“Nargis Fakhri”的第一行,而不返回任何其他参与者的第一行,我知道Find_In_set,但那是为了确定演员是否存在一年。

解决方案将和桌子设计一样难看

select
  id, name
from
  TheTable
where
  movieyears <> '[]' and
  replace(replace(replace(replace(replace(movieyears, '2011', ''), '2012', ''), '2013', ''), '2014', ''), ',', '') = '[]'
选择
身份证,姓名
从…起
表
哪里
电影年“[]”和
替换(替换)(替换(替换(电影年,'2011','','2012','','2013','','2014','','','','')=

请注意,您的年份似乎是有序的。如果是这种情况:

select t.*
from tablewithdata t
where substring_index(movieyears, ',', 1) >= '2011';

1.参见规范化。仅此而已。不要在一列中存储多个值。我希望可以,我在这里没有选择余地,是的,我知道规范化,我只是没有这样做的状态。丑陋的代码需要丑陋的伴侣,非常同意:)