我一直试图通过mysql查询从列中获取值。
该列(让我们谈谈颜色)有几个外键(没有外部约束)。假设一件衬衫可以有蓝色和黑色,列“颜色”将有"1,10“为1-蓝色和10-黑色,都是外键。
我想要创建查询来搜索是否使用了蓝色,这样我就可以防止从他的原始表中删除该颜色。虽然听起来很简单,但我不能用:
SELECT * FROM shirt WHERE color LIKE '%$id%'如果没有使用蓝色,那么它仍然会找到黑色(10),并给出查询的真实值。
发布于 2015-01-19 16:32:08
SELECT * FROM shirt WHERE FIND_IN_SET($id, color) > 0发布于 2015-01-19 16:42:57
好吧..。如果我正确理解你的问题。将这些值存储为逗号分隔字符串。你想要找出是否有一件t恤能保持这种颜色。
$colorToSearchFor = '1'; //blue.. sepearte it by comma to check for several colors at the same time e.g '1,10'
$sql = "SELECT * FROM shirt WHERE color IN(colorToSearchFor)";
//count the results, if it's more than one then a t-shirt has the specifik color assigned.建议有两个不同的表格。一件是t恤,另一件是颜色。
https://stackoverflow.com/questions/28029042
复制相似问题