row dependant variables in mysql query - mysql

I have a mysql database as follows. I am using it with phpmyadmin.
id time_came time_exit
0 2 3
1 3 5
5 5 1
7 1 10
9 1 8
I want another column as "wait" with the following logic,
foreach(i in time_came){
wait=count(time_came<i&&i<time_exit)
}
So then each column has a "wait" value too. I can do this with php. But I need to do this with mysql. I am confusing because "i" is varying for each row?
Thanks in advance.

Is this what you want?
select t.*,
(select count(*)
from table t2
where t2.time_came between t.time_came and t2.time_came < t.time_exit
) as wait
from table t;
EDIT:
To do the update, you need to use join:
update table t join
(select t.id,
(select count(*)
from table t2
where t2.time_came between t.time_came and t2.time_came < t.time_exit
) as wait
from table t
) as newval
on t.id = newval.id
set t.wait = newval.wait

Related

Update MYSQL Table from multiple rows in another table

I'm trying to update table yy from table xx results by doing sum.
For example (syntax is abstract):
update table_yy
set sum_of_x_and_y = (
(select sum(row_x) from table_xx where class_id=1)
+
(select sum(row_y) from table_xx where class_id=1) )
Table xx
row_id class_id row_x row_y
1 1 4 5
2 1 5 6
3 2 6 7
4 1 7 8
Table yy
class_id sum_of_x_and_y
1 35
2 13
but instead of setting the class_id manually, I would love to do something like inner-join update, but I'm working with 15k+ of records.
Here is a query that should do the job
UPDATE table_yy, (
SELECT class_id, SUM(table_xx.row_x + table_xx.row_y) AS sum_of_x_and_y
FROM table_xx
GROUP BY table_xx.class_id
) AS table_sum
SET table_yy.sum_of_x_and_y = table_sum.sum_of_x_and_y
WHERE table_yy.class_id = table_sum.class_id
Your approach is fine. You just want a correlated subquery:
update table_yy yy
set sum_of_x_and_y = (select sum(xx.row_x) + sum(xx.row_y)
from table_xx xx
where xx.class_id = yy.class_id
);
Under many circumstances, this will have better performance, particularly if you have an index on table_xx(class_id).

Please give some approach to achieve the data set for the following data set

In my table there is 5 distinct event 1,2,3,4,5. In attendee column there are attendees who attained these events. and in isThisuserHost column there is 0 and 1 flag. 1 for host who host the event and 1 for attendee who attain the event.I need to show the event where host is 2 and attendee is 4. In this table we can see that event 1,2,and 4 is those rows where host is 2 and attendee is 4. How to extract the result using a single query?
Try the following query:
select distinct eventid
from mytable m1
where 2 in (select attendee
from mytable m2
where m2.eventid = m1.eventid
and isTheuserHost = 1
)
and 4 in (select attendee
from mytable m3
where m3.eventid = m1.eventid
and isTheuserHost = 0
)
You need a selfjoin here:
select h.eventId
from myTable h
join myTable a using(eventId)
where h.isTheuserHost = 1
and a.isTheuserHost = 0
and h.attendee = 2
and a.attendee = 4
h and a are different aliases for the same table. h stands for hoster and a for anttendee with the corresponding condition for the isTheuserHost column. Then you can use that table as if it were two.

SQL Query, how to select a interval where the next value is current value +1, identifying the next gap and leave everything else untouched?

I'm making a program on java using sql querys and i'd like to update a sequence of ids adding 1 to each, but only in the sequence interval.
Let's say i have a table like this:
1 - a | 2 - b | 3 - c | 5 - e | 6 - f
And i'd like to "push" a's ID and subsequents forward by 1, but only while the difference between one another is still 1. The result would be like this:
2 - a | 3 - b | 4 - c | 5 - e | 6 - f
I know it is possible to do programatically, but is there a way to do this with SQL querys? If not, what would be the best way to do this?
Thanks in advance.
Edit: Found the answer thanks to Thorsten Kettner, my query ended up like this:
update pair set id = id + 1
where id >= 1
and id <
(
select min(id)
from (select * from pair) as gap
where gap.id > 1 and not exists
(
select *
from (select * from pair) as oneless
where oneless.id = gap.id - 1
)
)
order by id desc
If you know where the gap is that you want closed (ID 4 in your case) use this:
update mytable set id = id + 1 where id < 4;
If you don't know where the gap is, find it:
select min(id) - 1
from mytable
where not exists
(
select *
from mytable oneless
where oneless.id = mytable.id - 1
);
We can combine both statments now to do the update:
update mytable set id = id + 1
where id <
(
select min(id) - 1
from mytable
where not exists
(
select *
from mytable oneless
where oneless.id = mytable.id - 1
)
);
Let's call your table Pair and the two columns Key and Value. Your query would be something like this:
update p1
set Key = p1.Key + 1
from Pair as p1
inner join Pair as p2
on p2.Key = p1.Key + 1
PS. I'm assuming your result has a typo because after the update, f should have a value of 7.

Is there better way to do this query?

SELECT *
FROM a
WHERE a.re_id = 3443499
AND a.id IN
(
SELECT b.rsp_id FROM b
WHERE b.f_id = 9
GROUP BY b.rsp_id
HAVING FIND_IN_SET(16, GROUP_CONCAT(b.o_id)) > 0
AND FIND_IN_SET(15, GROUP_CONCAT(b.o_id)) > 0
UNION
SELECT b.rsp_id FROM b
WHERE b.f_id = 4
GROUP BY b.rsp_id
HAVING FIND_IN_SET(5, GROUP_CONCAT(b.o_id)) > 0
)
ORDER BY id DESC
Here "f_id" is array and its values are those in first parameter of "FIND_IN_SET" function.
For example
9=>(
16,
15
),
4=>(
5
)
Sample data for those 2 folumns in table b, 2 columns f_id and o_id
f_id o_id
9 15
9 18
9 23
4 5
3 8
The gist of this answer is that the current query does not run. So, fix the syntax and ask another question.
First, you could write the query so it is syntactically correct. The query will fail as written, because the first subquery returns at least two rows and the second only one.
Second, use UNION ALL instead of UNION, unless you specifically want to incur the overhead of removing duplicates.
Third, the ORDER BY will generate an error.
Fourth, the GROUP_CONCAT() is dangerous and unnecessary.
I'm not 100% sure this is the intention, but I would start with a query like this:
SELECT a.id, a.re_id
FROM a
WHERE a.re_id = 3443499 AND
a.id IN (SELECT b.rsp_id
FROM b
WHERE b.f_id = 9
GROUP BY b.rsp_id
HAVING MAX(b.o_id = 16) > 0 AND
MAX(b.o_id = 15) > 0
)
UNION ALL
SELECT b.rsp_id, NULL
FROM b
WHERE b.f_id = 4
GROUP BY b.rsp_id
HAVING MAX(b.o_id = 5) > 0
ORDER BY id;
Then, if you want this optimized, I would suggest asking another question, along with relevant information about the table structures and current performance.

Between Math and Sql Query

please help me to answer this question..
if I have database, table name is table1 and the field name is example its contain 5 numbers like this:
example
1
2
3
4
5
then I would like do this with database above:
1*2 = 2
3*2 = 6
4*6 = 24
5*24 = 120
120 * 10% = 12
What is the correct sql query to make that happen?
thank you very much
You can use user-defined variables in you query ,if multiplication result exceeds from 100 and you need to divide it by 10 you can do so
SELECT
CONCAT(example,'*',#mul) `expression`,
#mul := CASE
WHEN #mul* example >100
THEN (#mul* example/10)
ELSE #mul* example END
mul
FROM Table1,
(SELECT #mul:=1) p
ORDER BY `example`
Fiddle Demo
For your exactly the same desired result set this would work
SELECT * FROM
(SELECT
CONCAT(example,'*',#mul) `expression`,
#mul := #mul* example mul
FROM Table1,
(SELECT #mul:=1) p
ORDER BY `example`
) t
UNION
SELECT CONCAT(#mul,'*',' 10% ') `expression`,
(#mul/10) FROM (SELECT #mul) p1
Fiddle Demo