[英]detect change in column value?
This is how my table looks 这是我桌子的样子
page time
x 0
x 1
x 2
y 0
y 1
x 0
x 1
x 2
x 3
z 0
z 1
z 2
z 3
I am plotting a graph based on values of x,y,z. 我正在根据x,y,z的值绘制图形。
So My graph looks like-- 所以我的图看起来像-
The height of each bar is determined by peak value of respective page. 每个条的高度由相应页面的峰值确定。
I tried something like this 我尝试过这样的事情
`$query="select `page`,`time` from `table` where session_id="$session_id" GroupBy ...how do i go about this.";
but it gives me only 3 bars.But I want the no. 但它只给我3个小节。但我要不要。 of bar depending on the switch (switch from peak value to 0.In above table there are 4 such switch and thus there are 4 bars in my graph.) 的条形取决于开关(从峰值切换到0。在上表中有4个这样的开关,因此我的图形中有4条。)
I hope I am clear with my question. 我希望我清楚我的问题。 I am not able figure out the query. 我无法弄清楚查询。
UPDATE--> I am developing a tracker which basically keeps track of all the links clicked by a user and time spend on it.So the switch can be from X to Y to X to Z..It can be random.Thats why i am confused. 更新->我正在开发一个跟踪器,该跟踪器基本上可以跟踪用户单击的所有链接以及在其上花费的时间,因此切换可以从X到Y到X到Z.它可以是随机的。这就是为什么我很困惑。
UPDATE 2: suppose if I click a link that redirects me to page x when I have already spent time==2 at the very same page x...then only max(time) is returned,I need the previous max time on X + current max time on X. In simple words,If i visit the same page again..I dont want the last recorded time to get suppressed if if spend more time on it comparitively,Instead I want to add up the time.. 更新2:假设如果我已经在同一页面x上花费了时间== 2时单击了将我重定向到页面x的链接...则仅返回max(time),我需要X上的先前最大时间+当前X上的最大时间。用简单的话来说,如果我再次访问同一页面,那么。如果不想花费更多的时间,我不希望最后记录的时间被压制,相反,我想累加时间。
The problem is, that you want to group based on a specific order of the columns, not column page. 问题是,您要基于列的特定顺序(而不是列页面)进行分组。
Here's a workaround: 解决方法:
CREATE TABLE Table1
(id int auto_increment, `page` varchar(1), `time` int, primary key (id))
;
INSERT INTO Table1
(`page`, `time`)
VALUES
('x', 0),
('x', 1),
('x', 2),
('y', 0),
('y', 1),
('x', 0),
('x', 1),
('x', 2),
('x', 3),
('z', 0),
('z', 1),
('z', 2),
('z', 3)
;
select page, max(time) from (
select
t.*
, IF(@prev != page, @counter := @counter + 1, @counter) as my_groups
, @prev:=page
from
Table1 t
, (select @prev:=NULL, @counter:=0) v
order by id
) sq
group by my_groups
See it working live here in an sqlfiddle . 看到它在sqlfiddle中实时工作。
Note that I introduced the column id
. 请注意,我介绍了列id
。 It's just a column that helps to guarantee an order. 它只是一栏,有助于保证订单。 In a database there's no first or last row. 在数据库中,没有第一行或最后一行。 The order in which you inserted the rows is not the guaranteed order when you simply select from the table. 仅从表中选择时,不能保证插入行的顺序。
Explanatation: 说明:
Here 这里
Table1 t
, (select @prev:=NULL, @counter:=0) v
we initialize our variables inside the query. 我们在查询中初始化变量。 It's the same as writing 和写作一样
SET @prev:=NULL;
SET @counter:=0;
SELECT ... FROM Table1 ORDER BY whatever...
Now in the SELECT part of the subquery 现在在子查询的SELECT部分
, IF(@prev != page, @counter := @counter + 1, @counter) as my_groups
, @prev:=page
these 2 rows are important in the order they are written. 这2行按其写入顺序很重要。 The second row assigns the value of the read row to the variable @prev. 第二行将读取行的值分配给变量@prev。 In the first row we check if @prev is different from the row which is read by SELECT. 在第一行中,我们检查@prev是否与SELECT读取的行不同。 If yes, we increase our counter variable @counter by 1, if no we leave it as it is. 如果是,我们将计数器变量@counter加1,如果否,则保持原样。
The subquery (a bit modified to make it more clear what happens) 子查询(进行了一些修改以使其更加清楚发生了什么)
select
IF(@prev != page, @counter := @counter + 1, @counter) as my_groups
, page
, @prev
, @prev:=page
, time
from
Table1 t
, (select @prev:=NULL, @counter:=0) v
order by id
returns this result then: 然后返回此结果:
| MY_GROUPS | PAGE | @PREV | @PREV:=PAGE | TIME |
--------------------------------------------------
| 0 | x | (null) | x | 0 |
| 0 | x | x | x | 1 |
| 0 | x | x | x | 2 |
| 1 | y | x | y | 0 |
| 1 | y | y | y | 1 |
| 2 | x | y | x | 0 |
| 2 | x | x | x | 1 |
| 2 | x | x | x | 2 |
| 2 | x | x | x | 3 |
| 3 | z | x | z | 0 |
| 3 | z | z | z | 1 |
| 3 | z | z | z | 2 |
| 3 | z | z | z | 3 |
The outer query is simple then. 外部查询很简单。 We group by the column my_groups
(we aliased this row with it) 我们按my_groups
列my_groups
(我们为此行加上别名)
IF(@prev != page, @counter := @counter + 1, @counter) as my_groups
and for each group we return the page and the maximum time. 对于每个组,我们返回页面和最长时间。
Hope it's all clear now. 希望现在一切都清楚了。 Feel free to ask, though, if that's not the case :) 随意询问,如果不是这样的话:)
you can do something like: 您可以执行以下操作:
select page, max(time) from table where session_id = ...
group by page
but according to your table structure above, it will give only 3 values since 1st and 3rd x would be treated as same. 但是根据您上面的表格结构,由于第1个和第3个x会被视为相同,因此它只会给出3个值。 This is because of the way your table is defined. 这是因为表的定义方式。 So you will get 3 graphs and not 4. 因此,您将获得3个图形而不是4个图形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.