• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

一个SQL查询的初学者有关问题~

mysql 搞代码 7年前 (2018-06-01) 107次浏览 已收录 0个评论

一个SQL查询的菜鸟问题~请指教啊~~~
表名:[news]
字段:news_id,news_title,category

如果有一条数据category存放的内容为1,2,3,4,5,6
为什么下面的SQL语句查询不出数据
select   *   from   [news]   where   '1 '   in(category)   and   news_id=2

本来在MSSQL里可以用:
Select   *   from   [news]   where   CharIndex( ',1, ',   ', '   +   category   +   ', ')   >   0   and   news_id=2

但是在mysql里不行啊!

——解决方案——————–
mysql不支持[]吧
select * from `news` where '1 ' in(category) and news_id=2

我试了..可以的啊.
——解决方案——————–
[news]
MYSQL没这[]符号
——解决方案——————–
mysql> create table `[new]` (
-> news_id int not null auto_increment primary key,
-> new_title varchar(64) not null,
-> category int(11)
-> )
-> ;
Query OK, 0 rows affected (0.13 sec)

mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| [new] |
| a |
| auto_t |
| ip |
| song |
| song2 |
| song3 |
| song4 |
| t |
| t1 |
| t2 |
| test1 |
| total |
| v |
+—————-+
14 rows in set (0.02 sec)

mysql> insert into `[new]`(new_title,category) values( 'test1 ',1),
-> ( 'test2 ',1),
-> ( 'test3 ',3),
-> ( 'test4 ',4),
-> ( 'test5 ',5),
-> ( 'test6 ',6);
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0

mysql> select * from `[new]`;
+———+———–+———-+
| news_id | new_title | category |
+———+———–+———-+
| 1 | test1 | 1 |
| 2 | test2 | 1 |
| 3 | test3 | 3 |
| 4 | test4 | 4 |
| 5 | test5 | 5 |
| 6 | test6 | 6 |
+———+———–+———-+
6 rows in set (0.00 sec)

mysql> select * from `[new]` where find_in_set(1,category) and news_id = 2;
+———+———–+———-+
| news_id | new_title | category |
+———+———–+———-+
| 2 | test2 | 1 |
+———+———–+———-+
1 row in set (0.00 sec)

mysql> select * from `[new]` where 1 in (select category from `[new]`) and news_
id = 2;
+———+———–+———-+
| news_id | new_title | category |
+———+———–+———-+
| 2 | test2 | 1 |
+———+———–+———-+
1 row in set (0.00 sec)

mysql>

——解决方案——————–
注意“指的是ESC键下面的那个键。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:一个SQL查询的初学者有关问题~

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址