返回记录,而最喜欢的颜色并非全部排除

发布于 2025-02-11 11:16:38 字数 711 浏览 2 评论 0原文

mysql小提琴

CREATE TABLE IF NOT EXISTS `favorites` (
  `name` varchar(20) NOT NULL,
  `colors` varchar(20) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `favorites` (`name`, `colors`) VALUES
  ('Timmy', 'blue,yellow,green'),
  ('John', 'green,orange,black'),
  ('Alysha', 'red,purple,orange'),
  ('James', 'yellow,green'),
  ('Janet', 'reen'),
  ('Peter', 'purple,orange,blue'),
  ('Ryan', 'purple'),
  ('Tony', 'blue,red');

nofollow noreferrer“例如,根据我排除的颜色,如果我排除紫色,除了瑞安(Ryan)之外,所有的记录都将返回。

如果我排除黄色和绿色,那么除了詹姆斯之外,所有人都会回来。

如果我排除了橙色和黑色,所有记录都会返回,约翰之所以返回其余的唱片,是因为虽然他以橙色和黑色为他最喜欢的颜色,但他也喜欢绿色,这并不排除。

这是一种愚蠢的方法,我知道最喜欢的颜色应该存储在另一个桌子中,但是无法更改表结构,我试图使用纯SQL来完成此操作

mysql fiddle http://sqlfiddle.com/#!9/03f4e72

CREATE TABLE IF NOT EXISTS `favorites` (
  `name` varchar(20) NOT NULL,
  `colors` varchar(20) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `favorites` (`name`, `colors`) VALUES
  ('Timmy', 'blue,yellow,green'),
  ('John', 'green,orange,black'),
  ('Alysha', 'red,purple,orange'),
  ('James', 'yellow,green'),
  ('Janet', 'reen'),
  ('Peter', 'purple,orange,blue'),
  ('Ryan', 'purple'),
  ('Tony', 'blue,red');

I want to return records based on colors I exclude, for example, if I exclude purple, all but Ryan's record would return.

If I exclude yellow and green, all but James would return.

If I excluded orange and black, all records would return, the reason John would return with the rest of the records is because while he has orange and black as his favorite colors, he also likes green, which was not excluded.

It's a silly way of going about this and I know the favorite colors should be stored in another table, but the table structure can not be changed and I am trying to accomplish this using pure sql

Any help is appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文