简体   繁体   English

在PHP中一次从两个MySQL表中选择数据

[英]Selecting data from two MySQL tables at once in php

What I have tried to achieve; 我试图达到的目标;

Counting the number of rows in two tables (adding them together) grouping them by the country code in the Country column in the table. 计算两个表中的行数(将它们加在一起),并按表中“ Country列中的Country地区代码对行进行分组。 I will then output the results by the number of rows and country eg 然后,我将按行数和国家/地区输出结果,例如

  • GB => 200 views GB => 200次观看
  • US => 500 views 美国=> 500次观看
  • CH => 119 views CH => 119浏览

MySQL code I have so far; 到目前为止,我有MySQL代码;

SELECT COUNT(id) as TotalClicks, Country FROM adverts_analytics_user_clicks WHERE AdvertID =:AdvertID GROUP BY Country

This works well, however I need it to select the same data from another table at the same time and combine the COUNT() values according to the Country field. 这很好,但是我需要它同时从另一个表中选择相同的数据,并根据“ Country字段组合COUNT()值。

Any help would be appreciated! 任何帮助,将不胜感激!

Repeat your first query against the second table, such that the output coumns have the same name and datatype. 对第二个表重复您的第一个查询,以使输出列具有相同的名称和数据类型。

Then union those results together. 然后将这些结果结合在一起。

Finally Sum the total clicks by country. 最后,按国家汇总总点击次数。

SELECT Country, sum(TotalClicks) as TotalClicks
FROM 
(
SELECT COUNT(id) as TotalClicks, Country FROM adverts_analytics_user_clicks WHERE AdvertID =:AdvertID GROUP BY Country
UNION ALL
SELECT COUNT(id) as TotalClicks, Country FROM table 2 WHERE AdvertID =:AdvertID GROUP BY Country
)
GROUP BY Country

One way to do that is to append another query to your query with a UNION ALL operator. 一种方法是使用UNION ALL运算符将另一个查询追加到查询中。 (That query can reference a different table name; the important part is that it returns the same number of columns with the same datatypes.) Then wrap that in a set of parens, and assign it an alias, and then use that in place of a tablename in an outer query: (该查询可以引用不同的表名;重要的部分是它返回具有相同数据类型的相同数量的列。)然后将其包装在一组括号中,并为其分配一个别名,然后使用它代替外查询中的表名:

For example: 例如:

SELECT SUM(t.TotalClicks) AS TotalClicks, t.Country
  FROM (
         /* original query*/

         SELECT COUNT(id) as TotalClicks, Country
           FROM adverts_analytics_user_clicks
          WHERE AdvertID =:AdvertID
          GROUP BY Country

          /* append results from query that returns same columns */
          UNION ALL
          SELECT COUNT(id) as TotalClicks, Country
            FROM some_other_table
           WHERE AdvertID =:AdvertID2
           GROUP BY Country
      ) t
  GROUP BY t.Country

NOTE: we've encountered issues when the same bind variable name is used twice in the same statement. 注意:在同一条语句中两次使用相同的绑定变量名称时,我们遇到了问题。 We resorted to separate bind variable names, and binding the same value twice. 我们使用单独的绑定变量名称,并将相同的值绑定两次。

Because of the way that MySQL handles inline views (materializing it as a temporary table) it's almost always more efficient to get the aggregates from each table, and then concatenating the aggregates together, than it is to concatenate the non-aggregated tables. 由于MySQL处理内联视图(将其具体化为临时表)的方式,从每个表中获取聚合,然后将聚合连接在一起,比将非聚合表连接起来几乎总是更有效。


FOLLOWUP 跟进

simon funiic said... the statement is working, however it is not returning the right number for TotalClicks (there are two rows for Country GB - one in each table) but only 1 is returned. simon funiic说... 语句正在工作,但是它没有为TotalClicks返回正确的数字(Country GB有两行-每个表中有一行),但仅返回1。

response 响应

Given the GROUP BY on the outer query (in my answer above), we would expect only a single row returned for a Country value of 'GB' . 给定外部查询中的GROUP BY(在上面的答案中),我们期望仅返回一行,表示Country值为'GB' If you run just the query in the inline view (aliased as t in the query above) you would get both rows. 如果仅在内联视图中运行查询(在上面的查询中以t ),则将获得两行。

The outermost query is combining those two rows (with the GROUP BY t.Country), and adding the values from TotalClicks together ( SUM(t.TotalClicks) ) 最外面的查询是合并这两行(使用GROUP BY t.Country),并将TotalClicks的值加在一起( SUM(t.TotalClicks)

If you want the rows returned separately, there's no need for that outer query at all. 如果要分别返回行,则根本不需要该外部查询。

If you want to include a "tag" on each row to identify which query the row comes from, it's easy to add a literal in the SELECT list of each query: 如果要在每行上包含一个“标记”以标识该行来自哪个查询,可以在每个查询的SELECT列表中添加一个文字:

     SELECT COUNT(id) AS TotalClicks
          , Country
          , 'aauc' AS `source`
       FROM adverts_analytics_user_clicks
      WHERE AdvertID =:AdvertID
      GROUP BY Country
      UNION ALL
     SELECT COUNT(id) AS TotalClicks
          , Country
          , 'sot' AS `source`
       FROM some_other_table
      WHERE AdvertID =:AdvertID2
      GROUP BY Country
      ORDER BY 2,3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM