大家好,
有3张表
dict 字典表
(
dict_id 自增长ID,
keyword 关键字
)dict_to_product 对应关系表
(
dict_id,
product_id
)product 产品表
(
product_id,
product_title
)现在我想做一搜索,从dict中搜索关键字,查出符合条件的产品来。
keyword like '条件%'
分词的时候,如果有多个关键字,这时要取产品的交集,即符合所有条件的产品才取出来。
一条SQL能搞定吗?这个SQL怎么写?
我现在是分几次求出product_id, 然后用PHP数据求出它们的交集,觉得这样效率不高。
高手帮忙指点一下。
有3张表
dict 字典表
(
dict_id 自增长ID,
keyword 关键字
)dict_to_product 对应关系表
(
dict_id,
product_id
)product 产品表
(
product_id,
product_title
)现在我想做一搜索,从dict中搜索关键字,查出符合条件的产品来。
keyword like '条件%'
分词的时候,如果有多个关键字,这时要取产品的交集,即符合所有条件的产品才取出来。
一条SQL能搞定吗?这个SQL怎么写?
我现在是分几次求出product_id, 然后用PHP数据求出它们的交集,觉得这样效率不高。
高手帮忙指点一下。
解决方案 »
- mysql存储过程统计
- MYSQL中新增一条记录时,字段值得处理方法
- mysql中怎样截取字符串中数值
- 请问我是这样启动mysql3.23吗?
- 求个sql语句可以更新mediumtext类型的字段
- mysql报错Too high level of nesting for select 求解决
- text型字段怎么判断是否为空?
- 急!在线等。。。如何导出MySQL的表结构?
- mysql的数据更新后,如何通知其他进程进行刷新显示?
- MySql.Data.MySqlClient.MySqlException (0x80004005): error connecting: Timeout ex
- 怎么写根据一个字段来关联到相应的表呢?
- select username=str from cdb_members where uid=1;的问题
select product_title
from (dict_to_product dp inner join product p on dp.product_id=p.product_id)
inner join (
select dict_id
from dict
where keyword like '条件1%'
or keyword like '条件2%'
pr keyword like '条件3%') d on dp.dict_id=d.dict_id
group by product_title
having count(*)=3
dict数据
dict_id keyword
32321 pcga
32362 bp71
32363 bp71a
32364 bp71auc
32365 bp71ce7dict_to_product数据
product_id dict_id
214 32321
215 32321
216 32321
217 32321
216 32362
450 32362
216 32363
450 32363
product数据
product_id, product_title
214 产品1
215 产品2
216 产品3
217 产品4
450 产品5
当用户输入查询条件pcga bp71时,应该返回的正确结果是产品 216(产品3)
用一条SQL如何写?
dict_to_produc b on a.dict_id=b.dict_id
inner join product c on b.product_id=c.product_id
where a.keyword='pcga bp71'
select c.product_title from dict a inner join
dict_to_produc b on a.dict_id=b.dict_id
inner join product c on b.product_id=c.product_id
where a.keyword='pcga bp71'
+---------+---------+
| dict_id | keyword |
+---------+---------+
| 32321 | pcga |
| 32362 | bp71 |
| 32363 | bp71a |
| 32364 | bp71auc |
| 32365 | bp71ce7 |
+---------+---------+
5 rows in set (0.00 sec)mysql> select * from dict_to_product;
+------------+---------+
| product_id | dict_id |
+------------+---------+
| 214 | 32321 |
| 215 | 32321 |
| 216 | 32321 |
| 217 | 32321 |
| 216 | 32362 |
| 450 | 32362 |
| 216 | 32363 |
| 450 | 32363 |
+------------+---------+
8 rows in set (0.00 sec)mysql> select * from product;
+------------+---------------+
| product_id | product_title |
+------------+---------------+
| 214 | 产品1 |
| 215 | 产品2 |
| 216 | 产品3 |
| 217 | 产品4 |
| 450 | 产品5 |
+------------+---------------+
5 rows in set (0.00 sec)mysql>
mysql> select product_title
-> from (dict_to_product dp inner join product p on dp.product_id=p.product_id)
-> inner join (
-> select dict_id
-> from dict
-> where keyword = 'pcga'
-> or keyword = 'bp71') d on dp.dict_id=d.dict_id
-> group by product_title
-> having count(*)=2;
+---------------+
| product_title |
+---------------+
| 产品3 |
+---------------+
1 row in set (0.00 sec)mysql>
inner join (
select dict_id
from dict
where keyword = 'pcga'
or keyword = 'bp71') d
on
感谢。