优秀的编程知识分享平台

网站首页 > 技术文章 正文

mysql进行数据查询(单表查询)(mysql 查询数据表)

nanyue 2024-10-27 11:29:07 技术文章 3 ℃

Select 后接查询展示的字段,* 代表所有字段,from后接从哪张表里查找,where后面接查询条件。

例1:从fail_info 表中查找日期为1月13号的不良情况。

Select * from fail_info where line_date='1月3号';

例2:Select fail_name from fail_info where fail_category='功能';

Group by 后接分组字段,distinct用于字段去重,后接需要去重的字段。如查询各分类的不良数量。

Select distinct fail_category,sum(fail_qty) from fail_info group by fail_category;

having 是分组之后指定查询条件,如查询分类功能和外观的不良数量。

Select distinct fail_category,sum(fail_qty) from fail_info group by fail_category having fail_category not in(‘尺寸’);

注意where用于分组前指定条件,having用于分组后指定条件。例如:

Select distinct fail_category,sum(fail_qty) from fail_info where line_date=’1月3号’ group by fail_category;

order by 用于排序,后接指定的排序字段,limit用于指定展示的行。例如:

根据不良数量排序,升序,展示前5条数据。

select * from fail_info order by fail_qty limit 5;

根据不良数量排序,降序,展示前5条数据。

select * from fail_info order by fail_qty desc limit 5;

根据不良数量排序,降序,跳过前2条展示前3条数据。

Select * from fail_info order by fail_qty desc limit 2,3;

GROUP_CONCAT 将查询字段合并展示,如按分类查询不良产生的日期。例如:

select GROUP_CONCAT(distinct line_date) as ‘不良日期’,fail_category from fail_info group by fail_category;

请您关注我的账号,下篇文章我们分享多表关系的建立与查询。

最近发表
标签列表