mysql不知有没有IN和FIND_IN_SET的结合方法?
大概的要求:
查询条件:
$sort_id = '11,21,23'; 查询表:
id sort
1 11,22
2 21,23
3 50,60
4 23,60 查询结果:
id sort
1 11,22
2 21,23
4 23,60 ID 3 ,没有任何匹配,所以未被列出。 请问这样的MYSQL语句应该怎么写?find_in_set('11,21,23',sort )查了下手册,并不可以这样子的
大概的要求:
查询条件:
$sort_id = '11,21,23'; 查询表:
id sort
1 11,22
2 21,23
3 50,60
4 23,60 查询结果:
id sort
1 11,22
2 21,23
4 23,60 ID 3 ,没有任何匹配,所以未被列出。 请问这样的MYSQL语句应该怎么写?find_in_set('11,21,23',sort )查了下手册,并不可以这样子的
Query OK, 0 rows affected (0.05 sec)mysql> create table t5 (id int,sort varchar(30));
Query OK, 0 rows affected (0.09 sec)mysql> insert into t5 values
-> (1 ,'11,22'),
-> (2 ,'21,23'),
-> (3 ,'50,60'),
-> (4 ,'23,60');
Query OK, 4 rows affected (0.05 sec)
Records: 4 Duplicates: 0 Warnings: 0mysql> set @sort_id = '11,21,23';
Query OK, 0 rows affected (0.00 sec)mysql> select * from t5 where sort regexp replace(@sort_id,',','|');
+------+-------+
| id | sort |
+------+-------+
| 1 | 11,22 |
| 2 | 21,23 |
| 4 | 23,60 |
+------+-------+
3 rows in set (0.06 sec)mysql>
+------+-------+
| id | sort |
+------+-------+
| 1 | 11,22 |
| 2 | 21,23 |
| 4 | 23,60 |
+------+-------+
3 rows in set (0.00 sec)mysql> set @sort_id = '11,21,3';
Query OK, 0 rows affected (0.00 sec)mysql> select * from t5 where concat(',',sort,',') regexp concat(',',replace(@sort_id,',',',|,'),',');
+------+-------+
| id | sort |
+------+-------+
| 1 | 11,22 |
| 2 | 21,23 |
+------+-------+
2 rows in set (0.00 sec)mysql>
我现在是用了三次查询,感觉有点弱智,article
id sortid
1 3,4
2 2,3,7
3 12,17,19
4 1,2,6,21我现在想用(3,6,17)这三个类id分别调出各属于它的一篇文章,希望能一个语句完成
如:共需调三条记录,3、6及17各有一条属于它的记录,不知可有办法?
要求大概就是这样:表的数据:
id sortid
1 3,4
2 2,3,7
3 12,17,19
4 1,2,6,21固定要取的类别ID :3,6,17
我现在的做法是:
SELECT id,title FROM article WHERE find_in_set('3',sortid) ORDER BY adddate Desc LIMIT 1
SELECT id,title FROM article WHERE find_in_set('6',sortid) ORDER BY adddate Desc LIMIT 1
SELECT id,title FROM article WHERE find_in_set('17',sortid) ORDER BY adddate Desc LIMIT 1然后还要判断让它们不能为相同的
select * from tt where concat(',',sort,',') regexp concat(',',replace('3,6,17',',',',|,'),',');这是准确匹配