我有两个表,第一个是它有一些规格的产品清单的产品,另一方面,我有一个客户的表,他们想要什么样的产品,他们可能想要一个列表中的任何一个城镇的产品,正如下表所解释的,
产品表
| id | owner | userid | city | town | status | price |
| 1 | jon spee | 10 | 10 | 4 | 0 | 10500 |
| 2 | Hiss Roe | 10 | 7 | 9 | 0 | 20000 |
| 3 | John Smi | 10 | 10 | 12 | 0 | 10000 |客户表
| id | fullname | userid | city | towns | status | price |
| 1 | name 1 | 10 | 10 |4,8,6,2| 0 | 20000 |
| 2 | name 2 | 10 | 7 | 7,2,9 | 0 | 25000 |
| 3 | name 3 | 10 | 10 | 1 | 0 | 20000 |MySQL查询:
SELECT *
FROM clients
INNER JOIN products
ON (
clients.userid = products.userid AND
clients.price >= products.price AND
clients.city = products.city AND
clients.status = products.status我希望它也能在城镇中进行检查,比如在每个城镇执行这个查询(动态),
(products.town LIKE '%4%' OR products.town LIKE '%8%' OR products.town LIKE '%6%' OR products.town LIKE '%2%')发布于 2020-12-21 01:56:27
您可以使用此查询
SELECT *
FROM clients
INNER JOIN products
ON (
clients.userid = products.userid AND
clients.price >= products.price AND
clients.city = products.city AND
find_in_set(clients.town, products.town) AND
clients.status = products.status您还可以在php中获取它,并根据获取的结果创建语句。
发布于 2020-12-21 01:51:20
您的主要工作应该是修复您的数据模型。不要在字符串列中存储多个整数值。您应该有一个单独的表来存储客户端和城镇之间的关系,每个元组位于一个单独的行上。
这就是说:对于您当前的设计,您可以加入find_in_set()
on
clients.userid = products.userid
and ...
and find_in_set(product.town, client.towns)https://stackoverflow.com/questions/65386604
复制相似问题