how to MYSQL compare values from same columns and join table - mysql

how do i compare 2 table in mysql?
with the following table
name table ok1
id model name
1 AAA AAA
2 BBB BBB
3 CCC CCC
name table ok2
idok nameok
1 ok1
2 ok2
3 ok3
and I want to generate a query
id name model nameok idmodel namemodel modelmodel okname
1 AAA AAA ok1 2 BBB BBB ok2
1 AAA AAA ok1 3 CCC CCC ok3
2 BBB BBB ok2 1 AAA AAA ok1
2 BBB BBB ok2 3 CCC CCC ok3
3 CCC CCC ok3 1 AAA AAA ok1
3 CCC CCC ok3 2 BBB BBB ok2
the following sql I made, the query does not match what I want
sql query :
SELECT
t1.id,
t1.name,
t1.model,
t3.nameok as nameok1,
t2.id AS idmodel,
t2.name AS namemodel,
t2.model AS modelmodel,
t3.nameok as nameok2
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.name <> t2.name AND
t1.model <> t2.model;
inner join yourTable t1
ON
t1.id = t3.idok and
t2.id = t3.idok

You can use joins:
select ok1.*, ok1_2.*, ok2.* -- or whatever columns you want
from ok1 join
ok1 ok1_2
on ok1.id <> ok1_2.id join
ok2
on ok2.id = ok1_2.id;

Related

how can i sort data by rows? [MYSQL]

my database
id
title
create_by
1
aaa
2
2
bbb
1
3
ccc
3
4
ddd
2
5
eee
2
6
fff
3
what i want. sort by row that have same most value.
id
title
create_by
1
aaa
2
4
ddd
2
5
eee
2
3
ccc
3
6
fff
3
2
bbb
1
Thank you very much.
WITH cte AS ( SELECT id,
title,
create_by,
COUNT(create_by) OVER (PARTITION BY create_by) cnt
FROM src_table )
SELECT id,
title,
create_by
FROM cte
ORDER BY cnt, id;

how to MYSQL compare values from same columns

how do i compare 1 table in mysql?
with the following table
name table belajar
id model name
1 AAA AAA
2 BBB BBB
3 CCC CCC
and I want to generate a query
id name model idmodel namemodel modelmodel
1 AAA AAA 2 BBB BBB
1 AAA AAA 3 CCC CCC
2 BBB BBB 1 AAA AAA
2 BBB BBB 3 CCC CCC
3 CCC CCC 1 AAA AAA
3 CCC CCC 2 BBB BBB
It sounds like maybe a self join is what you want here:
SELECT
t1.id,
t1.name,
t1.model,
t2.id AS idmodel,
t2.name AS namemodel,
t2.model AS modelmodel
FROM yourTable t1
INNER JOIN yourTable t2
ON t1.name <> t2.name AND
t1.model <> t2.model;
ORDER BY
t1.id,
t2.id;
The join criteria is basically a cross join, except that you don't want to match two records if the names and models be the same.

Two column (different name, different table) as one row

is it possible to change these two tables
Employee A B C
Alex G G B
Martin B G G
...
------------------------------
Employee2 AA BB CC
Sam G B B
Max G G B
...
Into one table?
List Employee Mark
AAA Alex G
BBB Alex G
CCC Alex B
AAA Sam G
BBB Sam B
CCC Sam B
...
AA and A are different column in different table but I want to display them in one table with the same name (e.g. "AAA") and it must be in row not in column.
You can use a UNION query to unpivot the data from columns into rows:
SELECT 'AAA' AS List, Employee, A AS Mark
FROM t1
UNION ALL
SELECT 'BBB' AS List, Employee, B AS Mark
FROM t1
UNION ALL
SELECT 'CCC' AS List, Employee, C AS Mark
FROM t1
UNION ALL
SELECT 'AAA' AS List, Employee2, AA AS Mark
FROM t2
UNION ALL
SELECT 'BBB' AS List, Employee2, BB AS Mark
FROM t2
UNION ALL
SELECT 'CCC' AS List, Employee2, CC AS Mark
FROM t2
ORDER BY Employee, List
Output (for your sample data)
List Employee Mark
AAA Alex G
BBB Alex G
CCC Alex B
AAA Martin B
BBB Martin G
CCC Martin G
AAA Max G
BBB Max G
CCC Max B
AAA Sam G
BBB Sam B
CCC Sam B
Demo on SQLFiddle

How to get row occurrence value in MySQL?

I have a following table
id name amount
1 aaa 1000
2 bbb 1500
3 ccc 1700
4 ddd 2000
5 aaa 1400
6 aaa 1700
7 bbb 1800
What I need is one more column to display the occurrence value based on the name as follows
id name amount occurrence
1 aaa 1000 1
2 bbb 1500 1
3 ccc 1700 1
4 ddd 2000 1
5 aaa 1400 2
6 aaa 1700 3
7 bbb 1800 2
You can do it with a correlated subquery:
SELECT id, name, amount,
(SELECT COUNT(*)
FROM mytable AS t2
WHERE t2.name = t1.name AND t2.id <= t1.id) AS occurrence
FROM mytable AS t1
Demo here

MySQL: Update all rows in 2 table matching results of another query

I have two table with same id, i'd like to change the id for both table with the value from 2nd table.
Ex:
Table_1
ID TITLE CONTENT AUTHOR
1 aaa abcd aaaa
2 aaa abcd aaaa
3 aaa abcd aaaa
4 aaa abcd aaaa
Table_2:
ID post_id post_title pos_key value
1 1 aaa a_key 2
2 1 aaa E_key 2000
3 2 aaa b_key 3
4 2 aaa b_key 3
5 2 aaa E_key 2500
6 3 aaa c_key 4
7 3 aaa d_key 5
8 3 aaa E_key 3000
9 4 aaa f_key 6
10 4 aaa E_key 3500
How can i update all Table_1 ID and Table_2 post_id from the value of Table_2 which have E_key on post_key row ?.
This is what i want for my both table:
Table_1
ID TITLE CONTENT AUTHOR
2000 aaa abcd aaaa
2500 aaa abcd aaaa
3000 aaa abcd aaaa
3500 aaa abcd aaaa
Table_2:
ID post_id post_title pos_key value
1 2000 aaa a_key 2
2 2000 aaa E_key 2000
3 2500 aaa b_key 3
4 2500 aaa b_key 3
5 2500 aaa E_key 2500
6 3000 aaa c_key 4
7 3000 aaa d_key 5
8 3000 aaa E_key 3000
9 3500 aaa f_key 6
10 3500 aaa E_key 3500
What have you tried? A good start is to write a SELECT query which pulls both the old and new values; from there it's usually simple to rewrite it as an UPDATE. I believe this will do it:
UPDATE T1
SET ID = T2.Value
FROM Table_1 AS T1
INNER JOIN Table_2 AS T2 ON T1.ID = T2.post_id
WHERE
T2.pos_key = 'E_key'
UPDATE T2
SET post_id = NewIDs.Value
FROM Table_2 AS T2
INNER JOIN Table_2 AS NewIDs ON T2.post_id = NewIDs.post_id
WHERE
NewIDs.pos_key = 'E_key'
My apologies if my MySQL is off, I usually work in MS SQL.