GVKun编程网logo

包含多个JOINS和GROUP BY CLAUSE的查询中的COUNT(多个包含条件)

7

在这篇文章中,我们将带领您了解包含多个JOINS和GROUPBYCLAUSE的查询中的COUNT的全貌,包括多个包含条件的相关情况。同时,我们还将为您介绍有关1055-Expression#1ofOR

在这篇文章中,我们将带领您了解包含多个JOINS和GROUP BY CLAUSE的查询中的COUNT的全貌,包括多个包含条件的相关情况。同时,我们还将为您介绍有关1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contai、Clickhouse groupArray/groupUniqArray/arrayJoin/splitByChar、Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre、Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ''use...的知识,以帮助您更好地理解这个主题。

本文目录一览:

包含多个JOINS和GROUP BY CLAUSE的查询中的COUNT(多个包含条件)

包含多个JOINS和GROUP BY CLAUSE的查询中的COUNT(多个包含条件)

我正在处理包含3个表的数据库:

  1. 公司名单
  2. 他们出售的产品表
  3. 他们在每个日期提供的价格表

我在php中进行这样的查询,以生成在特定日期提供特定产品类型最低价格的公司列表。

SELECT  a.name AS company,  c.id,  MIN(c.price) AS apyFROM `companies` aJOIN `company_products` b ON b.company_id = a.idJOIN `product_prices` c ON c.product_id = b.idWHERE  b.type = "%s"  AND c.date = "%s"GROUP BY a.idORDER BY c.price ASCLIMIT %d, %d

这为我获取了我需要的数据,但是为了在PHP中实现寻呼机,我需要知道总共有多少家公司提供该产品。LIMIT表示我只看到前几个…

我尝试将SELECT子句更改为SELECT COUNT(a.id)或SELECT
COUNT(DISTINCT(a.id)),但这些似乎都不给我我想要的东西。我尝试在我的计数查询中删除GROUP BY和ORDER
BY,但这也不起作用。有任何想法吗?

答案1

小编典典

在我看来,您应该GROUP BY a.id, c.id-按组进行分组a.id仅意味着通常c.id每个s有几个s
a.id,而您只是得到其中一个“随机的”。这似乎是基本正确性的问题。一旦您解决了该问题,则初始的SELECT COUNT(*)FROMetc等绝对应为您提供以下查询将返回的行数,因此您可以相应地准备您的寻呼机。

1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contai

1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contai

最近将mysql升级了,发现navicat中运行 sql语句报错:

解决办法见下文(转载):

转载于:http://m.mamicode.com/info-detail-2570625.html

之前一直使用的mysql5,突然换成8之后,有许多地方不一样,今天就碰到一个。

在使用sql语句创建表时,报错:

 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column ‘information_schema.PROFILING.SEQ‘ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

 

 

网上找了下处理的方法,大体有两种:一种是直接改配置文件(永久性的),另一种是使用sql语句来设置(临时性)

修改配置文件:在配置文件里加一句就可以了:sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

以上语句不要写错哦,错了服务启动不了,我就是因为写错搞了好久

  windows的在安装目录里找到my.ini,添加进去后,关掉mysql服务重启

    关:net stop mysql     

              启:net start mysql     

    (注意:mysql是你服务的名称)

       centos7在/etc/my.cnf中添加,重启

           service mysqld restart或者 systemctl restart mysqld

在window和centos7上,我都设置了,之后就不会报错 ^_^

我在创建表时,报了这个错,但还是创建成功了,不晓得是为甚 ?

 

Clickhouse groupArray/groupUniqArray/arrayJoin/splitByChar

Clickhouse groupArray/groupUniqArray/arrayJoin/splitByChar

行列转置一般由由行转为列,或者由列转为行。

CREATE TABLE datasets.t_city
(
    `province` String, 
    `city` String, 
    `createtime` DateTime, 
    `city_level` Int8
)
ENGINE = MergeTree()
ORDER BY province
SETTINGS index_granularity = 8192;
 
 
 
insert into t_city values('Hubei','Wuhan',now(),2),('Hubei','Xiangyang',now(),3),('Shanghai','Shanghai',now(),1),('Guangdong','Guangzhou',now(),1),('Guangdong','Shenzhen',now(),1),('Guangdong','DOngguan',now(),2),('Guangdong','Zhuhai',now(),3);
 
 
Clickhouse> select * from t_city;
 
SELECT *
FROM t_city
 
┌─province──┬─city──────┬──────────createtime─┬─city_level─┐
│ Guangdong │ Guangzhou │ 2020-07-07 14:02:53 │          1 │
│ Guangdong │ Shenzhen  │ 2020-07-07 14:02:53 │          1 │
│ Guangdong │ DOngguan  │ 2020-07-07 14:02:53 │          2 │
│ Guangdong │ Zhuhai    │ 2020-07-07 14:02:5

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

原文链接:https://blog.csdn.net/hq091117/article/details/79065199

https://blog.csdn.net/allen_tsang/article/details/54892046

MySQL 5.7.5后only_full_group_by成为sql_mode的默认选项之一,这可能导致一些sql语句失效。

比如下表game:

 

id group_id name score
1 A 小刚 20
2 B 小明 19
3 B 小花 17
4 C 小红 18

执行sql:
select name, group_id from game group by group_id(记为sql_make_group)
返回:

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ''game.name'' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

解决方法

  1. 把group by字段group_id设成primary key 或者 unique NOT NULL。这个方法在实际操作中没什么意义。

  2. 使用函数any_value把报错的字段name包含起来。如,select any_value(name), group_id from game group by group_id

  3. 在配置文件my.cnf中关闭sql_mode=ONLY_FULL_GROUP_BY.。msqyl的默认配置是sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION。可以把ONLY_FULL_GROUP_BY去掉,也可以去掉所有选项设置成sql_mode=,如果你确信其他选项不会造成影响的话。

成功的步骤:

命令行打开mysql.cnf,可以用whereis查找

sudo vim /etc/mysql/conf.d/mysql.cnf

滚动到文件底部复制并粘贴

[mysqld] 
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

wq保存并退出

重启MySQL 

sudo service mysql restart

执行成功后,返回结果应该是

name group_id
小刚 A
小明 B
小红 C

为什么默认设置ONLY_FULL_GROUP_BY限制?

对于上述的报错信息,我的理解是select字段里包含了没有被group by条件唯一确定的字段name。

因为执行sql_make_group语句实际上把两行纪录小明小花合并成一行,搜索引擎不知道该返回哪一条,所以认为这样的sql是武断的(arbitrary).

解决办法2和3都是禁止检查返回结果的唯一性。

进阶讨论

上述办法可以解决报错的问题,但也说明sql语句本身有逻辑问题。name字段不应该出现在返回结果,因为它是不确定的。

考虑这样的需求:按group_id分组后,找出每组得分最少的人。

执行sql: select any_value(name) as name, group_id, min(score) as score from game group by group_id order by min(score)

返回结果

name group_id score
小明 B 17
小红 C 18
小刚 A 20

B组的name小明(因为小明的id更小),而期望结果应该是小花

所以单纯使用group by无法实现这样的需求。可以使用临时表的方法:

select id, name,t.group_id, t.score from (select group_id, min(score) as score from game group by group_id order by min(score)) t inner join game on t.group_id=game.group_id and t.score=game.score

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ''use...

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ''use...

<div id="content_views"> <!-- flowchart 箭头图标 勿删 --> <svg xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block"></path></svg> <p>安装了mysql5.7,用group by 查询时抛出如下异常:</p>

<prename="code"><code>Expression <span>#3</span> <span>of</span> <span>SELECT</span> list <span>is</span> <span>not</span> <span>in</span> <span>GROUP</span> <span>BY</span> clause <span>and</span> contains nonaggregated column <span>''userinfo.t_long.user_name''</span> which <span>is</span> <span>not</span> functionally dependent <span>on</span> columns <span>in</span> <span>GROUP</span> <span>BY</span> clause; this <span>is</span> incompatible <span>with</span> sql_mode=only_full_group_by</code></pre>

<h3 id="原因"><a name="t0"></a>原因:</h3>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MySQL 5.7.5和up实现了对功能依赖的检测。如果启用了only_full_group_by SQL模式(在默认情况下是这样),那么MySQL就会拒绝选择列表、条件或顺序列表引用的查询,这些查询将引用组中未命名的非聚合列,而不是在功能上依赖于它们。(在5.7.5之前,MySQL没有检测到功能依赖项,only_full_group_by在默认情况下是不启用的。关于前5.7.5行为的描述,请参阅<a href="https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html" rel="nofollow" target="_blank">MySQL 5.6参考手册</a>。)</p>

<p>执行以下个命令,可以查看 sql_mode 的内容。</p>

<prename="code"><code>mysql&gt; SHOW SESSION VARIABLES;</code></pre>

<prename="code"><code>mysql&gt; SHOW <span>GLOBAL</span> VARIABLES;</code></pre>

<prename="code"><code>mysql&gt; <span>select</span> <span>@@</span>sql_mode;</code></pre>

<p>可见session和global 的sql_mode的值都为: <br> <code>ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION</code></p>

<p>only_full_group_by说明: <br> <a href="http://blog.csdn.net/u014520745/article/details/76056170" rel="nofollow" target="_blank">only_full_group_by :使用这个就是使用和oracle一样的group 规则, select的列都要在group中,或者本身是聚合列(SUM,AVG,MAX,MIN) 才行,其实这个配置目前个人感觉和distinct差不多的,所以去掉就好</a> <br> 官网摘抄: <br> 官网:<a href="https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_only_full_group_by" rel="nofollow" target="_blank">ONLY_FULL_GROUP_BY</a> <br> Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns.</p>

<p>As of MySQL 5.7.5, the default SQL mode includes ONLY_FULL_GROUP_BY. (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default. For a description of pre-5.7.5 behavior, see the MySQL 5.6 Reference Manual.)</p>

<p>A MySQL extension to standard SQL permits references in the HAVING clause to aliased expressions in the select list. Before MySQL 5.7.5, enabling ONLY_FULL_GROUP_BY disables this extension, thus requiring the HAVING clause to be written using unaliased expressions. As of MySQL 5.7.5, this restriction is lifted so that the HAVING clause can refer to aliases regardless of whether ONLY_FULL_GROUP_BY is enabled.</p>

<h3 id="解决"><a name="t1"></a>解决:</h3>

<p>执行以下两个命令:</p>

<prename="code"><code>mysql&gt; <span>set</span> <span>global</span> sql_mode=<span>''STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'';</span></code></pre>

<prename="code"><code>mysql&gt; <span>set</span> session sql_mode=<span>''STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION''</span>;</code></pre>

<p>这两个命令,去掉 sql_mode 的 ONLY_FULL_GROUP_BY</p>

<p>见其他文章有说: <br> <code>直接修改mysql配置文件(我的系统是Ubuntu16.04的,在/etc/mysql/mysql.conf.d/mysqld.cnf 中并没有sql_mode这个配置,所以直接加上就好,如果是其他系统有得修改就不用添加了)</code> <br> 这个方法暂时没有式。</p>

<p><strong>mysql 配置信息读取顺序。</strong></p>

<p>①ps aux|grep mysql|grep ‘my.cnf’</p>

<p>②mysql –help|grep ‘my.cnf’</p>

<p>/etc/my.cnf, /etc/mysql/my.cnf, /usr/local/etc/my.cnf, ~/.my.cnf 这些就是mysql默认会搜寻my.cnf的目录,顺序排前的优先。mysql按照上面的顺序加载配置文件,后面的配置项会覆盖前面的。</p>

<p>如果没有该文件可以自定义一个文件。然后回默认读取配置中的内容) <br> 查看你需要修改的是哪个配置文件。我只有/etc/my.cnf 只修改这个文件即可</p>

<p>配置文件my.cnf通常会分成好几部分,如[client],[mysqld], [mysql]等等。MySQL程序通常是读取与它同名的分段部分,例如服务器mysqld通常读取[mysqld]分段下的相关配置项。如果配置项位置不正确,该配置是不会生效的</p>

<p>参考:<a href="https://stackoverflow.com/questions/37951742/1055-expression-of-select-list-is-not-in-group-by-clause-and-contains-nonaggr" rel="nofollow" target="_blank">https://stackoverflow.com/questions/37951742/1055-expression-of-select-list-is-not-in-group-by-clause-and-contains-nonaggr</a></p>

<p>这个语句没试过,先记录:</p>

<prename="code"><code><span><span>set</span> @@sql_mode=<span>''STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION''</span>;</span></code></pre>

<p>去掉ONLY_FULL_GROUP_BY即可正常执行sql.</p> </div>

我们今天的关于包含多个JOINS和GROUP BY CLAUSE的查询中的COUNT多个包含条件的分享已经告一段落,感谢您的关注,如果您想了解更多关于1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contai、Clickhouse groupArray/groupUniqArray/arrayJoin/splitByChar、Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre、Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ''use...的相关信息,请在本站查询。

本文标签: