MySQL SELECT语句,其中value在数组中

use*_*858 1 php mysql arrays select

我需要一些MySQL声明的帮助,我无法真正开始工作.

我有这样一张桌子:

+---+-------------------------------+
|id | fruit                         |
+---+-------------------------------+
| 1 | apple,orange,pear,grape,lemon |
| 2 | pear,melon,apple,kiwi,lemon   |
| 3 | orange,kiwi,pear,apple,cherry |
| 4 | grape,lemon,cherry,apple,melon|
+---+-------------------------------+
Run Code Online (Sandbox Code Playgroud)

我需要做的是选择列fruit包含单词的所有行melon.有问题的单词可能位于数组中的任何位置.

我厌倦了下面的查询但由于某种原因我只得到3-4行,绝对不是全部:

$fruit = $_GET['fruit'];
$query1= "SELECT * FROM tbl_fruits WHERE ".$fruit." IN (fruit)";
Run Code Online (Sandbox Code Playgroud)

任何意见将不胜感激.

jue*_*n d 6

您可以使用 FIND_IN_SET

SELECT * FROM tbl_fruits 
WHERE find_in_set('$fruit', fruit)
Run Code Online (Sandbox Code Playgroud)

但实际上你应该改变你的桌面设计.

切勿在一列中存储多个值!

一个更好的桌子设计

fruits table
------------
id    name
1     melon
2     orange
3     apple
...


products table
-------------------
id    name   price
1     P1     1.50
2     P2     2.99
3     P3     0.99


product_fruits table
--------------------
product_id   fruit_id
1            1
1            2
2            2
3            1
Run Code Online (Sandbox Code Playgroud)

这是一个经典的多对多关系(m到n).