MySQL的select语句如何使用(mysql,select,开发技术)

时间:2024-05-02 15:27:14 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :
MySQL的select语句如何使用
select语句可 以用回车分隔
$sql="select*fromarticlewhereid=1"和$sql="select*fromarticlewhereid=1"都可以得到正确的结果,但有时分开写或许能更明了一点,特别是当sql语句比较长时。
批量查询数据
可以用in来实现$sql="select*fromarticlewhereid;in(1,3,5)"
使用concat连接查询的结果
$sql="selectconcat(id,"-",con)asresfromarticlewhereid=1"返回"1-articlecontent"
使用locate
用法:selectlocate("hello","hellobaby");返回1不存在返回0
使用group by
以前一直没怎么搞明groupby和orderby,其实也满简单的,groupby是把相同的结果编为一组exam:$sql="selectcity,count(*)fromcustomergroupbycity";这句话的意思就是从customer表里列出所有不重复的城市,及其数量(有点类似distinct)groupby经常与AVG(),MIN(),MAX(),SUM(),COUNT()一起使用
使用having
having允许有条件地聚合数据为组$sql="selectcity,count(*),min(birth_day)fromcustomergroupbycityhavingcount(*)>10";这句话是先按city归组,然后找出city地数量大于10的城市btw:使用groupby+having速度有点慢同时having子句包含的表达式必须在之前出现过
组合子句
where、groupby、having、orderby(如果这四个都要使用的话,一般按这个顺序排列)
使用distinct
distinct是去掉重复值用的$sql="selectdistinctcityfromcustomerorderbyiddesc";这句话的意思就是从customer表中查询所有的不重复的city
使用limit
如果要显示某条记录之后的所有记录$sql="select*fromarticlelimit100,-1";
多表查询
$sql="selectuser_namefromuseru,membermwhereu.id=m.idandm.reg_date>;=2006-12-28orderbyu.iddesc"

注意:如果user和 member两个标同时有user_name字段,会出现mysql错误(因为mysql不知道你到 底要查询哪个表里的user_name),必须指明是哪个表的;

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:MySQL的select语句如何使用的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Linux中ssh-keygen命令怎么用下一篇:

3 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18