您的位置:首页 > 数据库

整理出的数据库中几个常用sql查询语句

2006-12-28 17:49 771 查看
1、最简单的单条件查询,where指定查询条件。

select messagetype,messagename from t_sr_message where messagename='manage'

下面列举几个查询条件,不是全部:



2、分组查询

select superno,typename from t_sr_messagetype group by SUPERNO,typename having typename<'200'
//根据superno,typename分组返回superno和typename2个列的记录,并且要满足typename<200这个条件。

3、排序查询


select * from t_sr_messagetype order by DESC

order by 可以指定查询结果如何排序。DESC表示倒序,asc表示顺序

4、转换查询


select userid 用户编号, username 用户名称, userpwd 用户密码 from userinfo

查询结果如下:

用户编号用户名称用户密码
1sword123456
2niuniu654321
5、计算查询


select logincount+exitcount from userinfo

6、等值多表查询
等值多表查询将按照等值的条件查询多个数据表中关联的数据,要求关联的多个数据表的某些字段具有相同的数据类型、宽度和取值范围


select A.name,B.typename from A,B where A.Id = B.Id

查询出A表中Id与B表中Id相同的记录
非等值多表查询与此类似。

7、一个带‘in’的嵌套查询

select news.newtitle from news where news.USERID in (select userid from userinfo where
username <> 'sword')

查询出news表中的newtitle字段的记录
查询结果要满足条件:先看后面的,查询出userinfo表中username不等于‘sword’的所有userid,然后再得出news表中userid在前面得到的userid里的所有news.newtitle记录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: