简体   繁体   English

Select 来自不同表的数据限制10,而其他表限制5

[英]Select data from different tables limit 10, while the other table limit 5

I am trying to use a MySQL query to select data from three tables, as following:我正在尝试对来自三个表的 select 数据使用 MySQL 查询,如下所示:

pv光伏
t_id t_id
t_name t_name

pv_values pv_values
v_id v_id
t_id t_id
value价值
datetime约会时间

alm阿尔姆
alm_id alm_id
t_id t_id
datetime约会时间
status地位
priority优先

I want to get the last 10 values from pv_values for the last 5 rows of alm that have priority "1", and also get the name from pv for those values.我想从pv_values中获取优先级为“1”的最后 5 行alm的最后 10 个值,并从pv中获取这些值的名称。

This is my query, but it only gives me the 10 last values of the last alm value:这是我的查询,但它只给了我最后一个alm值的最后 10 个值:

SELECT pv_values.*, pv.tag_name
FROM `pv_values`,`pv`
WHERE pv_values.`tag_id` = (
    SELECT `tag_id`
    FROM `alarms` as `alt`
    WHERE `alt`.`tag_id` = `pv`.`tag_id` and `alt`.`priority` = 1
    ORDER BY `alarm_id` DESC
    LIMIT 1
)

Limit 10

I expect to get:我希望得到:

pv_values.v_id pv_values.t_id pv_values.value pv_values.datetime pv.t_name 
1              1
2              1
3              1
4              1
5              1
6              1
7              1
8              1
9              1
10             1
1              2
2              2
3              2
4              2
5              2
6              2
7              2
8              2
9              2
10             2

where t_id came from the alm.t_id last 5.其中t_id来自最后 5 个alm.t_id

I like to work with with Data that you select, so that you can understand it better, waht i am doing, when you recognized algorithsm.我喜欢使用 select 的数据,这样当您识别算法时,您可以更好地理解它,我在做什么。

So with this data所以有了这个数据

CREATE TABLE alarmes
    (`ID` int, `tag_id` int,  `alarm` varchar(8))
;

INSERT INTO alarmes
    (`ID`,`tag_id`,  `alarm`)
VALUES
    (1,1,  'Alarm1'),
    (2,2, 'Alarm2'),
    (3,3,  'Alarm3'),
    (4,4,  'Alarm4')
;
CREATE TABLE pv_values
    (`ID` int, tag_id int ,  `values` varchar(8))
;

INSERT INTO pv_values
    (`ID`, `tag_id`, `values`)
VALUES
    (1,1 , 'value1'),
    (2, 1, 'value2'),
    (3, 1, 'value3'),
    (4, 1, 'value4'),
    (5,1 , 'value5'),
    (6, 1, 'value6'),
    (7, 1, 'value7'),
    (8, 1, 'value8'),
    (9,1 , 'value9'),
    (10, 1, 'value10'),
    (11, 1, 'value11'),
    (12, 1, 'value12'),
    (1,4 , 'value1'),

    (2, 5, 'value2'),
    (3, 4, 'value3'),
    (4, 4, 'value4'),
    (5,4 , 'value5'),
    (6, 4, 'value6'),
    (7, 4, 'value7'),
    (8, 4, 'value8'),
    (9,4 , 'value9'),
    (10, 4, 'value10'),
    (11, 4, 'value11'),
    (12, 4, 'value12'),
        (1,2 , 'value1'),
    (2, 2, 'value2'),
    (3, 2, 'value3'),
    (4, 2, 'value4'),
    (5,2, 'value5'),
    (6, 2, 'value6'),
    (7, 2, 'value7'),
    (8, 2, 'value8'),
    (9,2, 'value9'),
    (10, 2, 'value10'),
    (11, 2, 'value11'),
    (12, 2, 'value12'),
    (1,3 , 'value1'),
    (2, 3, 'value2'),
    (3, 3, 'value3'),
    (4, 3, 'value4'),
    (5,3 , 'value5'),
    (6, 3, 'value6'),
    (7, 3, 'value7'),
    (8, 3, 'value8'),
    (9,3 , 'value9'),
    (10, 3, 'value10'),
    (11, 3, 'value11'),
    (12, 3, 'value12')
;

You can use in mysql 5.x this statement您可以在 mysql 5.x 中使用此语句

SELECT rnk, t1.`tag_id`, `values`,alarm
FROM
(Select 
  *
  ,IF(@tag_id<>tag_id, @rank := 0, @rank := @rank +1) rnk
  ,@tag_id := tag_id
From pv_values,(SELECT @rank := 1) r,(SELECT @tag_id := 0) re
Where tag_id in (SELECT tag_id FROM alarmes)) t1 inner join alarmes alm on alm.tag_id = t1.tag_id  
WHERE rnk < 10;

or With this for mysql 8.x或使用此用于 mysql 8.x

SELECT rnk, t1.`tag_id`, `values`,alarm
FROM
(Select 
  *
  , ROW_NUMBER() OVER (
 PARTITION BY tag_id
ORDER BY tag_id ASC) rnk
From pv_values
Where tag_id in (SELECT tag_id FROM alarmes)) t1 inner join alarmes alm on alm.tag_id = t1.tag_id  
WHERE rnk < 11;

In both cases you get在这两种情况下,您都会得到

rnk     tag_id  values  alarm
0       1       value1  Alarm1
1       1       value2  Alarm1
2       1       value3  Alarm1
3       1       value4  Alarm1
4       1       value5  Alarm1
5       1       value6  Alarm1
6       1       value7  Alarm1
7       1       value8  Alarm1
8       1       value9  Alarm1
9       1       value10 Alarm1
0       2       value1  Alarm2
1       2       value2  Alarm2
2       2       value3  Alarm2
3       2       value4  Alarm2
4       2       value5  Alarm2
5       2       value6  Alarm2
6       2       value7  Alarm2
7       2       value8  Alarm2
8       2       value9  Alarm2
9       2       value10 Alarm2
0       3       value1  Alarm3
1       3       value2  Alarm3
2       3       value3  Alarm3
3       3       value4  Alarm3
4       3       value5  Alarm3
5       3       value6  Alarm3
6       3       value7  Alarm3
7       3       value8  Alarm3
8       3       value9  Alarm3
9       3       value10 Alarm3
0       4       value1  Alarm4
1       4       value3  Alarm4
2       4       value4  Alarm4
3       4       value5  Alarm4
4       4       value6  Alarm4
5       4       value7  Alarm4
6       4       value8  Alarm4
7       4       value9  Alarm4
8       4       value10 Alarm4
9       4       value11 Alarm4

As you can see it is selected 10 Rows for every tag_id.如您所见,它为每个 tag_id 选择了 10 行。

this is made by giving all rows for every tag_id a rownumber and then select the first 10.这是通过给每个 tag_id 的所有行一个行号然后 select 前 10 个行来实现的。

You have to join your table pv and ordering pv_values to your needs.您必须加入您的表pv并根据您的需要订购 pv_values。

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

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