auto increment for each foreignKey - mysql

I have a table that have UNIQUE id auto increment, and a foreign Key to another table, I want to add the Table col that will be auto increment foreach foreignKey.
example:
if the table ampty after this query:
INSERT INTO `table` (job_id) VALUES (1),(1),(1),(2),(2),(1),(2),(3) ;
the table shold look like this:
id | job_id | id2
0 1 1
1 1 2
2 1 3
3 2 1
4 2 2
5 1 4
6 2 3
7 3 1
I try to to set id2 null when I insert row, and just after it run this query:
UPDATE `table` AS t1 SET t1.`rid2` = ( SELECT COUNT( t2.`id` )
FROM `table` AS t2
WHERE t2.`job_id` = t2.`job_id`
AND t2.`id` < t1.`id` )
WHERE r1.`d2` = NULL
any suggestions?

Related

How to insert new rows for each foreign key from a table

I want to make a query to add for each user all the id_job, id_role pairs from the user with id 0 (id_user, default user). Is it possible in mysql?
user_role table
id_user
id_job
id_role
0
1
1
0
2
2
1
1
1
1
2
2
2
3
1
These are the dates I want to get to. As you can see in the first table, for the user with id 2, we don't have id_job and id_role [(1,1), (2,2)] and I want to run a query that populates the missing pairs for this user.
user_role_updated table
id_user
id_job
id_role
0
1
1
0
2
2
1
1
1
1
2
2
2
3
1
2
1
1
2
2
2
Thank you in advance!
insert into user_role(id_user, id_job, id_role)
with cte_0 as(select id_user
, id_job
, id_role
from user_role
where id_user = 0)
select distinct ur.id_user
, cte_0.id_job
, cte_0.id_role
from user_role ur
, cte_0
where ur.id_user <> 0
and (ur.id_user, cte_0.id_job, cte_0.id_role) not in (select id_user, id_job, id_role from user_role where id_user <> 0)
Here is a demo

MySQL: How to add a column for original_id?

I want to add a column original_id to existing table, for example table_1:
sub_id
next_sub_id
2
4
4
6
6
null
8
10
10
null
to table_2 as sub_id = 4 or sub_id = 6 belongs to the first one that sub_id = 2
and sub_id = 10 belongs to sub_id = 8
sub_id
next_sub_id
original_id
2
4
2
4
6
2
6
null
2
8
10
8
10
null
8
How can I query under MySQL? Thanks!
You can check this link for more detail information
sameple is like this
ALTER TABLE table
ADD [COLUMN] column_name column_definition [FIRST|AFTER existing_column];
then you can update the value in the new column
sample is like this
UPDATE [LOW_PRIORITY] [IGNORE] table_name
SET
column_name1 = expr1,
column_name2 = expr2,
...
[WHERE
condition];
You can use a recursive CTE for this:
with recursive cte as (
select sub_id, next_sub_id, sub_id as original_id, 1 as lev
from t
where not exists (select 1 from t t2 where t2.next_sub_id = t.sub_id)
union all
select t.sub_id, t.next_sub_id, cte.original_id, lev + 1
from t join
cte
on cte.next_sub_id = t.sub_id
)
select *
from cte
order by sub_id;
Here is a db<>fiddle.

How to conditionally set value for mysql row group? [duplicate]

I want to generate a new column in my table that is true if a row exists with certain conditions.
name | col1 | col2 | flag
--------------------------
a 1 2 0
a 2 3 0
b 1 2 0
b 4 3 0
Lets say I want to set the flag to 1 for every name identifier if a row exists with that name and where col1=2 and col2 = 3. So this would result in:
name | col1 | col2 | flag
--------------------------
a 1 2 1
a 2 3 1
b 1 2 0
b 4 3 0
because for a a row with col1=2 and col2 = 3 exists, but for b, such a row doesn't exist.
In pseudocode I want something like this:
ALTER TABLE table_name
ADD flag TINYINT(1)
IF ##row with condition col1=value1 and col2=value2 exists#
GROUP BY name
How can I generate this column?
So you want just to get those values from db? or you want to add column? those are 2 different goals.
So if you need just to get those values you can:
http://sqlfiddle.com/#!9/65b4c2/1
SELECT t.*, t2.flag
FROM table_name t
LEFT JOIN (
SELECT name, MAX(IF(col1=2 AND col2=3,1,0)) flag
FROM table_name
GROUP BY name
) t2
ON t.name = t2.name
and if you really need to add new column then you go this way:
http://sqlfiddle.com/#!9/226fb3/1
ALTER TABLE table_name ADD COLUMN flag TINYINT;
UPDATE table_name t
LEFT JOIN (
SELECT name, MAX(IF(col1=2 AND col2=3,1,0)) flag
FROM table_name
GROUP BY name
) t2
ON t.name = t2.name
SET t.flag=t2.flag

How to set MYSQL column to certain value if a certain row exists?

I want to generate a new column in my table that is true if a row exists with certain conditions.
name | col1 | col2 | flag
--------------------------
a 1 2 0
a 2 3 0
b 1 2 0
b 4 3 0
Lets say I want to set the flag to 1 for every name identifier if a row exists with that name and where col1=2 and col2 = 3. So this would result in:
name | col1 | col2 | flag
--------------------------
a 1 2 1
a 2 3 1
b 1 2 0
b 4 3 0
because for a a row with col1=2 and col2 = 3 exists, but for b, such a row doesn't exist.
In pseudocode I want something like this:
ALTER TABLE table_name
ADD flag TINYINT(1)
IF ##row with condition col1=value1 and col2=value2 exists#
GROUP BY name
How can I generate this column?
So you want just to get those values from db? or you want to add column? those are 2 different goals.
So if you need just to get those values you can:
http://sqlfiddle.com/#!9/65b4c2/1
SELECT t.*, t2.flag
FROM table_name t
LEFT JOIN (
SELECT name, MAX(IF(col1=2 AND col2=3,1,0)) flag
FROM table_name
GROUP BY name
) t2
ON t.name = t2.name
and if you really need to add new column then you go this way:
http://sqlfiddle.com/#!9/226fb3/1
ALTER TABLE table_name ADD COLUMN flag TINYINT;
UPDATE table_name t
LEFT JOIN (
SELECT name, MAX(IF(col1=2 AND col2=3,1,0)) flag
FROM table_name
GROUP BY name
) t2
ON t.name = t2.name
SET t.flag=t2.flag

UPDATE FROM SELECT with foreign key on parent with one query

How to update (change from first select table value second) second_table.first_table_id if first_table.email match in both select.
If it even possible. With one query!
----------------------------------------- UPDATE -----------------------------------------
EXAMPLE:
I need to update foreign key of second table if email field match in first table. I need to compare two query results with different parent_id (parents are in in same table with children)
table_1
-------------------------
| id | parent_id | email |
-------------------------
1 NULL NULL
2 NULL NULL
3 1 joe#m.ru
4 2 bob#f.ly
5 1 bob#f.ly
6 2 kira#.us
table_2
----------------
| id | first_id |
----------------
1 3
2 4
3 5
4 6
I have two parents with ids 1 and 2 and some children (ids: 3,4,5,6).
Also, keep in mind: 1 - old, 2 - new
Task: change foreign key in second table if children email with parent_id = 1 and chilren email with parent_id = 2 match (are the same).
In our example in second table row with id = 3 its foreign key field - first_id has to change from 5 to 4.
Following might get you started
UPDATE Table_2 t2u
SET first_id = (
SELECT t2.first_id
FROM Table_2 t2
INNER JOIN Table_1 t1 ON t1.id = t2.first_id
INNER JOIN (
SELECT parent_id = MAX(parent_id), email
FROM Table_1
GROUP BY
email
) t1p ON t1p.email = t1.email
INNER JOIN Table_1 t1i ON t1i.email = t1p.email
AND t1i.parent_id = t1p.parent_id
WHERE t2u.first_id <> t1i.id)
Test script (SQL Server)
;WITH Table_1 (id, parent_id, email) AS (
SELECT 1, NULL, NULL
UNION ALL SELECT 2, NULL, NULL
UNION ALL SELECT 3, 1, 'joe#m.ru'
UNION ALL SELECT 4, 2, 'bob#f.ly'
UNION ALL SELECT 5, 1, 'bob#f.ly'
UNION ALL SELECT 6, 2, 'kira#.us'
)
, Table_2 (id, first_id) AS (
SELECT 1, 3
UNION ALL SELECT 2, 4
UNION ALL SELECT 3, 5
UNION ALL SELECT 4, 6
)
SELECT t2.*, t1i.id as [update with]
FROM Table_2 t2
INNER JOIN Table_1 t1 ON t1.id = t2.first_id
INNER JOIN (
SELECT parent_id = MAX(parent_id), email
FROM Table_1
GROUP BY
email
) t1p ON t1p.email = t1.email
INNER JOIN Table_1 t1i ON t1i.email = t1p.email
AND t1i.parent_id = t1p.parent_id
WHERE t2.first_id <> t1i.id
Output
id first_id update with
----------- ----------- -----------
3 5 4