简体   繁体   English

SELECT,如何将某些行合并为一个,首先显示具有特定列值的行,并排除行

[英]SELECT, how to combine some rows as one, show rows with specific column value first and exclude a row

I have two tables, units and power . 我有两个表, unitspower Take a look at: 看一眼:

The power table: power表:

+----+-------+
| id | power |
+----+-------+
|  1 |     2 |
|  2 |     4 |
|  3 |     6 |
|  4 |     8 |
+----+-------+

The units table: units表:

 +----+---------+----------+--------+----------+---+---+
    | id | user_id | power_id | amount | group_id | x | y |
    +----+---------+----------+--------+----------+---+---+
    |  1 |      10 |        3 |   1000 |        0 | 1 | 1 |
    |  2 |      10 |        3 |   1000 |        0 | 1 | 1 |
    |  3 |      10 |        3 |   1000 |        0 | 1 | 1 |
    |  4 |      11 |        2 |    100 |        4 | 1 | 1 |
    |  5 |      11 |        2 |    100 |        4 | 1 | 1 |
    |  6 |      11 |        1 |    100 |        4 | 1 | 1 |
    |  7 |      12 |        4 |   1000 |        0 | 1 | 1 |
    +----+---------+----------+--------+----------+---+---+

I want to get result looking like this: 我想得到如下结果:

+----------+--------------+-------------+----------+---------+--+
| units.id | total_amount | total_power | group_id | user_id |  |
+----------+--------------+-------------+----------+---------+--+
|        3 |         1000 |        6000 |        0 |      10 |  |
|        2 |         1000 |        6000 |        0 |      10 |  |
|        4 |          300 |        1000 |        4 |      11 |  |
|        7 |         1000 |        8000 |        0 |      12 |  |
+----------+--------------+-------------+----------+---------+--+

Explanation: 说明:

  1. Exclude a specific id, should work same for both a single row and a row which is a sum of multiple rows in the table. 排除特定的ID,对于单行和表中多行之和的行应相同。 As you see, in the result set, row with id 1 was excluded. 如您所见,在结果集中,排除了ID为1的行。 The id is provided by the app, I think it's better to do it in MySQL than PHP, because MySQL could just discard this row (I think), but with PHP it would have to do if() checking for every loop iteration, which seems less efficient. id由应用程序提供,我认为在MySQL中比在PHP中更好,因为MySQL可以丢弃此行(我认为),但是对于PHP,它将必须执行if()检查每个循环迭代,这似乎效率较低。

  2. Show the summed-rows row before single rows, every time. 每次都在单行之前显示汇总行。

  3. Show user's units before other users', every time. 每次都在其他用户之前显示用户单位。 You can see that when rows with user_id of 10 (imagine this user is the one seeing the page) appear first in my result set. 您可以看到,当user_id10的行(假设该用户是看到页面的用户)出现在我的结果集中。

  4. Show units with highest power first. 首先显示功率最高的设备。

  5. Show units with highest amount first. 首先显示金额最高的单位。

  6. Show units with highest id first. 首先显示具有最高ID的单位。

The last(4.5.6) are sorted in regards to the priority of the result set, with 4th having the most of it. 根据结果​​集的优先级对last(4.5.6)进行排序,其中第4个具有最多的优先级。 I have this query: 我有这个查询:

SELECT   units.id,
         units.amount*power.power AS total_power,
         Sum(units.amount)        AS total_amount
FROM     units
JOIN     power
ON       units.power_id = power.id
WHERE    x = ?
AND      y = 1
GROUP BY group_id
ORDER BY group_id desc, total_power desc, total_amount desc, units.id desc limit ?,?

But it combines all units where group_id is 0, however I want units with group_id=0 to be separate rows in the result set. 但是它组合了group_id为0的所有单位,但是我希望group_id=0单位成为结果集中的单独行。 How to do that? 怎么做? Thanks! 谢谢!

Edit : To answer @Linoff's question about how I determine which id to exclude, I exclude the 1 in the example because a user always will see the result set through accessing it with a unit_id, which, again, in my example happens to be 1 . 编辑 :要回答@Linoff有关如何确定要排除的ID的问题,我在示例中排除了1 ,因为用户始终可以通过使用unit_id访问它来查看结果集,在我的示例中碰巧又是1 I hope it is clear now. 我希望现在已经清楚了。 Edit : The user can access this list of units on page " index.php?page=unit/view&id= . Then I SELECT the entered id separately for the purpose of my app, and then SELECT the list. But as I already have data for the entered id (for instance, 1 in this case) I do not need to have them when I SELECT from the units and power . 编辑 :用户可以在页面“ index.php?page = unit / view&id =上访问此单元列表。然后我为我的应用程序分别选择输入的ID,然后选择列表。但是由于我已经有数据对于输入的ID(例如,本例中为1),当我从unitspower选择时,不需要它们。

@Anonymous, to answer your question 1, answer 1 is: all rows with same group_id (except 0 which is a not-a-group marker) are grouped and combined together, so rows which are id 4 , 5 and 6 which have identical group_id are combined. @Anonymous,回答您的问题1,答案1是:用相同GROUP_ID所有行(除了0这是一个not-a-group标志物)被分组并组合在一起,所以行其是ID 456具有相同group_id被合并。 My problem is, I don't know how to exclude grouping for rows which are stand alone (no group marking) and how to sort the result so the rows with specified user_id are sorted first and grouped rows (4,5,6-turned-to-4) are also sorted first, but in user_id=10-first,user_id=??-second hierarchy, if this makes sense. 我的问题是,我不知道如何排除独立行的分组(无分组标记)以及如何对结果进行排序,以便将具有指定user_id的行排在首位并分组(排成4、5、6的行) -to-4)也将首先排序,但如果有意义,则以user_id = 10-first,user_id = ??-second的层次结构排序。

I think this is what you want : 我想这就是你想要的:

CREATE TABLE Power  (id int,  power int);
CREATE TABLE demo (id integer primary key, name varchar(20), hint text);
CREATE TABLE units (id, user_id, power_id, amount, group_id, x, y);

SELECT   1 as spec2_HIDETHIS,  /* Spec 2 show the groupedrows first */
         units.id    as units_ID,
         units.amount*power.power AS total_power,
         units.amount        AS total_amount,
         units.group_id      as group_id
FROM     units
JOIN     power
ON       units.power_id = power.id
WHERE    units.group_id=0  /*  */
AND      units.id != 1  /* Spec no 1 : exclud specific id */
union 
SELECT   0 as spec2_HIDETHIS,  /* Spec 2 show the groupedrows first */
         min(units.id) as units_ID ,
         units.amount*power.power AS total_power,
         Sum(units.amount)        AS total_amount,
         units.group_id           as group_id
FROM     units
JOIN     power
ON       units.power_id = power.id
where    units.group_id > 0
AND      units.id != 1  /* Spec no 1 : exclud specific id */
GROUP BY group_id
ORDER BY spec2_HIDETHIS, group_id desc, total_power desc, total_amount desc, units_ID desc  

Fiddle Link , you need to click on run to get the results. Fiddle Link ,您需要单击run以获得结果。

What I do not understand is spec 2 : "show the grouped before the non grouped. " That means that units id 4 should appear first in the list which it doesn't in your example. 我不明白的是规范2:“在未分组之前先显示分组。”这意味着单元ID 4应该首先出现在列表中,而不是在您的示例中。

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

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