MySQL: calculate and update a row with other rows value? - mysql

Table1
ID X Y
1 0.5 0.5
2 ? ?
3 5 5
Hello All,
Basically I have a table just like the one above. Im trying to UPDATE the value of X and Y of ID = 2 with the result of a division of the values of other colums.
I tried running this query without much success,
"UPDATE Table1 SET X = X IN (WHERE ID = 1) / Y IN (WHERE ID = 3) WHERE ID = 2"
The result that I want would be
ID X Y
1 0.5 0.6
2 (0.5/6) (0.6/5)
3 5 6

In many databases, you can use a JOIN. In MySQL syntax, this looks like:
UPDATE Table1 t1 JOIN
Table1 t1_1
ON t1_1.ID = 1 JOIN
Table1 t1_3
ON t1_3.ID = 3
SET t1.X = t1_1.X / t1_3.X,
t1.Y = t1_3.X / t1_3.Y
WHERE t1.ID = 2;
Similar logic can be accomplished with using subqueries, but the code is a bit messier.

Related

select maximum value for each returned record in mysql

My tables look something like this
table 1 :
relid BID
1 1
1 2
1 3
table 2:
id BID priority info
1 1 0 Json string
2 1 1 **
3 1 2 ****
4 2 0 ***
5 2 1 ****
6 3 0 ****
the question is that I want to select all the BID's with only the highest priority from table2 using inner join with table1, which means I want to get this result
id BID Priority info
3 1 2 Json String info
5 2 1 ***
6 3 0 ***
I've used this query and it works fine but with large numbers of records it works too slow !! the fetching time may last up to 70 seconds in MySQL which is a disaster for my server !!!
select * from table1 inner join table2 on table1.BID = table2.BID where table1.relid = 1 and table2.priority = (select max(priority) as m from table2 where table1.BID = table2.BID)
anyone has other suggestions that might work with better performance!
Consider the following:
SELECT y.id
, y.bid
, y.priority
, y.info
FROM table1 x
JOIN table2 y
ON x.BID = y.BID
JOIN (SELECT bid, MAX(priority) max_priority from table2 GROUP BY bid) z
ON z.bid = y.bid
AND z.max_priority = y.priority
WHERE x.relid = 1;
For further improvements, we'd really need to see proper CREATE TABLE statements for all relevant tables as well as the result of the EXPLAIN

SQL Query - Find Duplicates with a Different Key

I have the following data:
id userid name group
1 1 A x
2 1 A y
3 1 A z
4 2 B x
5 2 B y
6 3 C y
7 4 D x
8 5 E x
9 5 E z
10 6 F x
I want to find those records that meet all this condition:
Select all rows where the a userid belongs to a group other than y but the userid also belongs to group y.
The resulting dataset will be as follows:
id userid name group
1 1 A x
3 1 A z
4 2 B x
If you see, it has resulted in two records for userid a because these are two two records belong to groups other than y but the userid 1 also belongs to group y. Same for userid 2.
I have been breaking my head on how to get this in an SQL statement but not even close to a solution.
Any help is appreciated.
Use a join:
SELECT t1.*
FROM mytable t1
INNER JOIN mytable t2
ON t1.user_id = t2.user_id AND t1.group <> t2.group AND t2.group = 'y'
I think that would be the fastest query (but please feel free to try the other solutions as well).
Add an index on user_id if not already there and maybe play with some other indexes as well (maybe a composite index on group and user_id can be utilized)
Use exists
select *
from MyTable a2
where name_group <> 'y'
and exists (select 1
from MyTable a2
where a2.name_group = 'y'
and a2.userid = a1.userid)
You can get all the users that meet the condition using aggregation and having:
select userid
from t
group by userid
having sum( group = 'y' ) > 0 and
sum( group <> 'y') > 0;
I leave it to your to put this into a query to get all the original rows.

Distinct pairs MySQL

I need to find distinct pairs(x,y) from a table consisting 2 columns(X, Y).
The numbers which are repeated in the column shouldn't be included in the result set.
Table:
X Y
1 2
2 3
3 4
4 4
5 2
5 6
7 9
Result Set:
X Y
2 3
7 9
5 repeated in X and 2,4 in Y, so they will not form pairs with the corresponding Y and X.
The question was asked to me in an interview. Not able to find a solution. Need a query for this. Please help!
SELECT X, Y
FROM yourTable
WHERE X IN (SELECT X FROM yourTable GROUP BY X HAVING COUNT(*) = 1) AND
Y IN (SELECT Y FROM yourTable GROUP BY Y HAVING COUNT(*) = 1)
#strawberry appears to have found a way to do this using joins, but the option which popped into my head was to simply use non-correlated subqueries to find the X and Y values which appear only once in each respective column. Then, just use a WHERE clause to check that each X and Y value falls in these sets.
E.g.:
SELECT DISTINCT a.*
FROM my_table a
LEFT
JOIN my_table b
ON b.x = a.x
AND b.y <> a.y
LEFT
JOIN my_table c
ON c.y=a.y
AND c.x <> a.x
WHERE b.x IS NULL
AND c.x IS NULL;

Return items present in one MySQL table that are not present in the other with 2 columns to consider

I want to return all the postcodes in table1 that are active and that dont have any items in table2 that share the same coordinates (lat,lng).
I.e. in the below return :
AB11AC
I know there are several method where you are just checking one column, but not sure how to adapt for 2 columns. Should I just concatenate the 2 columns together in the query or is there a more efficient method? My tables each have around 2 million entries.
table1:
postcode lat lng active
-------------------------
AB11AA 55 1 Y
AB11AB 56 1 Y
AB11AC 57 1 Y
table2:
postcode lat lng active
--------------------------
AB11AA 55 1 Y
AB11AD 56 1 Y
AB11AE 59 1 Y
You can use a LEFT JOIN:
select *
from table1 t1
left join table2 t2
on t1.lat = t2.lat
and t1.lng = t2.lng
where t1.active = 'Y'
and t2.postcode is null
See SQL Fiddle with Demo
Or you can use a NOT EXISTS in the WHERE clause:
select *
from table1 t1
where t1.active = 'Y'
and not exists (select *
from table2 t2
where t1.lat = t2.lat
and t1.lng = t2.lng)
See SQL Fiddle with Demo

Search many props in mysql table

There is a table with 2 columns:
x y
1 1
1 2
1 3
2 2
2 3
2 4
3 1
3 2
3 5
If I need to find all X, where Y = 5. I use query
SELECT * FROM table WHERE y = 5
But, I need to find all X, where Y = 2 AND Y = 3.
How to do this on 1 query?
In result I must to get X = 1 and 2 (NOT 3!). And Y may be many
If i use SELECT * FROM table WHERE y IN (1, 2)
I will get x = 1, 2, 3, but I not need Y = 3 because in table no line x=3, y=3
SELECT x
FROM tableName
WHERE y IN (2,3)
GROUP BY X
HAVING COUNT(*) = 2
SQLFiddle Demo
if unique contraint was not specified for each x, then DISTINCT is a must
SELECT x
FROM tableName
WHERE y IN (2,3)
GROUP BY X
HAVING COUNT(DISTINCT y) = 2
SQLFiddle Demo
Try this:
SELECT * FROM TABLE b WHERE Y = 2 AND EXISTS (SELECT * FROM TABLE WHERE b.X = X AND Y = 3);
You need to use OR to create a boolean expression in your WHERE clause
SELECT * FROM table WHERE (y = 2) OR (y = 3);
If that gets tedious because of many y values, then use IN
SELECT * FROM table WHERE y IN (2,3);