查找数组的内部联接 mysql
Finding inner join mysql with array
我有两个 table,第一个是产品,其中包含具有某些规格的产品列表,另一方面我有一个 table 与客户以及他们想要什么类型的产品, 他们可能想要列表中任何城镇的产品,完全按照以下 tables,
中的说明
产品Table喜欢
| 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 |
客户Table喜欢
| 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%')
您的主要工作应该放在修复数据模型上。不要在字符串列中存储多个整数值。您应该有一个单独的 table 来存储客户和城镇之间的关系,每个元组在单独的行中。
也就是说:对于您当前的设计,您可以加入 find_in_set()
:
on
clients.userid = products.userid
and ...
and find_in_set(product.town, client.towns)
你可以使用这个查询
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 中获取它并根据获取的结果创建您的语句
我有两个 table,第一个是产品,其中包含具有某些规格的产品列表,另一方面我有一个 table 与客户以及他们想要什么类型的产品, 他们可能想要列表中任何城镇的产品,完全按照以下 tables,
中的说明产品Table喜欢
| 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 |
客户Table喜欢
| 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%')
您的主要工作应该放在修复数据模型上。不要在字符串列中存储多个整数值。您应该有一个单独的 table 来存储客户和城镇之间的关系,每个元组在单独的行中。
也就是说:对于您当前的设计,您可以加入 find_in_set()
:
on
clients.userid = products.userid
and ...
and find_in_set(product.town, client.towns)
你可以使用这个查询
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 中获取它并根据获取的结果创建您的语句