insert if exist on another table mysql on a specific value - mysql

I have 2 tables.
Table1 has 2 columns, id and city with 4 rows (names of city).
id
city
1
surabaya
2
jakarta
3
bandung
Table2 has 4 columns, id, city, produk and price.
id
city
produk
price
1
depok
apel
1500
2
pekanbaru
jeruk
2000
3
Nasional
mangga
2200
I have 1 record on table2 that has value "Nasional" on column city
How can I update this 1 row and insert the names of the city on table1
expected result
id
city
produk
price
1
depok
apel
1500
2
pekanbaru
jeruk
2000
3
surabaya
mangga
2200
4
jakarta
mangga
2200
5
bandung
mangga
2200
How can I achieve this
INSERT INTO table2
WHERE EXISTS (
SELECT name FROM table1 WHERE name='nasional'
);

I believe this is what you need:
UPDATE table2
SET name="nasional"
WHERE table2.name = table1.name;

Related

select the min value of a id from other table

I want to select the min value of a id from other table
here my table1
id
grades
1
1
2
2
1
3
1
4
2
5
my table2
id
name
1
andy
2
lucy
3
kevin
I tried this
select table2.id, name, min(table1.grades) as grade from table1, table2
but it only shows 1 record
what I expected
id
name
grade
1
andy
1
2
lucy
2
3
kevin
0(or null?)
thanks
SELECT id, table2.name, MIN(table1.grades) grade
FROM table1
JOIN table2 USING (id)
GROUP BY 1,2;

Swap column values across 2 different tables

I have two tables and need to swap the values of a column in each table - I can do this when they are in the same table but when I try to do this with different tables then the second value is already overwritten so gets lost.
For example:
table1
id user_id currency col2 col3......
1 1 10 Bob 2018-04-16
2 2 150 Tom 2018-05-17
3 3 60 Phil 2018-06-04
4 4 125 Jon 2017-12-01
5 5 35 Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 USD 16 Active
2 2 USD 17 Active
3 3 GBP 21 Left
4 4 CAD 16 Active
5 5 AUD 19 Active
I need these to look like:
table1
id user_id currency col2 col3......
1 1 USD Bob 2018-04-16
2 2 USD Tom 2018-05-17
3 3 GBP Phil 2018-06-04
4 4 CAD Jon 2017-12-01
5 5 AUD Mike 2018-07-21
table2
id user_id salary col2 col3......
1 1 10 16 Active
2 2 150 17 Active
3 3 60 21 Left
4 4 125 16 Active
5 5 35 19 Active
I tried:
UPDATE table1 t1, table2 t2
SET t1.currency=t2.salary, t2.salary=t1.currency
WHERE t1.user_id=t2.user_id;
but this does not work (currency gets set correctly but not the salary), is it possible to do?
Swap two columns values between two tables looked like a possible solution but the solution is changing table names as all the columns need swopped whereas I only need single columns swapped.
I believe you'll need to use a mix of both DDL and DML to do this.
First off you'll need to rename one of the columns to be swapped and add a column to hold the new value:
alter table table1 change currency salary int;
alter table table1 add currency varchar(3) after salary;
then update each table independently:
update table1 t1, table2 t2
set t1.currency = t2.salary
where t1.user_id = t2.user_id;
update table1 t1, table2 t2
set t2.salary = t1.salary
where t1.user_id = t2.user_id;
and finally remove the extra column:
alter table table1 drop salary;

SQL Distinct Group By Query

Sample table data's
id id_order name phone price
1 4E0 A 789 $100
2 4E0 A 789 $100
3 4LK A 789 $200
4 2LP B 420 $50
5 DK2 B 420 $80
i want result be like the rows of distinct (id_order) where phone = "789"
id id_order name phone price
1 4E0 A 789 $100
3 4LK A 789 $200
or
id id_order name phone price
2 4E0 A 789 $100
3 4LK A 789 $200
I tried this but didn't get desired output:
SELECT DISTINCT (id_order), * from table_name WHERE phone= "789";
select
min(id) id,
id_order,
name,
phone,
price
from yourtable
group by id_order, name, phone, price
To always return one row per id_order - even if the name, phone or price is different - have a sub-query that returns each id_order's lowest id. JOIN with that result:
select t1.*
from table_name t1
join (select min(id) minid, id_order
from table_name
group by id_order) t2
on t1.id = t2.minid and t1.id_order = t2.id_order
where t1.phone = 789
Executes as:
SQL>select * from table_name;
id id_order name phone price
=========== ======== ==== =========== ===========
1 4E0 A 789 100
2 4E0 A 789 100
3 4LK A 789 200
4 2LP B 420 50
5 DK2 B 420 80
5 rows found
SQL>select t1.*
SQL&from table_name t1
SQL&join (select min(id) minid, id_order
SQL& from table_name
SQL& group by id_order) t2
SQL& on t1.id = t2.minid and t1.id_order = t2.id_order
SQL&where t1.phone = 789;
id id_order name phone price
=========== ======== ==== =========== ===========
1 4E0 A 789 100
3 4LK A 789 200
2 rows found

how to get rows between two string values in mysql

My table looks like this:
id city road_id
-------------------------
1 london 3
2 manchester 3
3 newcastle 3
4 glasgow 3
5 london 5
6 newcastle 5
I know values of two cities and road_id and need something like this:
UPDATE table SET anothercolumn=1 WHERE id>=(id for)london AND id<(id for)glasgow AND road_id=3
to affect only these rows:
1 london 3
2 manchester 3
3 newcastle 3
UPDATE your_table
SET anothercolumn = 1
WHERE id >= (select id from your_table where city = 'london')
AND id < (select id from your_table where city = 'glasgow')
AND road_id = 3

Update using data from another table

I want the minimum price from Table 2 to be filled in price column of Table 1 for a particular id.
Table 1
pid price
111 0
222 0
333 0
Table 2
pid price
111 100
111 200
222 120
222 90
333 200
333 150
Expected output: Table 1
pid price
111 100
222 90
333 150
You would do something like:
UPDATE Table1 t
SET t.price = (SELECT MIN(t2.price) FROM Table2 t2 WHERE t2.pid = t.pid);
this is query to get lowest price from table2
(SELECT price FROM table2 WHERE price = ( SELECT MIN(price) FROM table2 ) ) now you can update the table 1 ( update table1 set price="result you got from above query" where id=given id)