Automatic sequence number (1,2, …) to be filled in a specific field - mysql

I have a view table as below and I want to make the custom field named “number” to have as automatic sequence number (1,2,….) to be filled in the column CUSTOMFIELDVALUE according to REQUESTID
enter image description here
I need a trigger code on the WOCUSTOMFIELD table that do what I want
Thanks in Advance
Lubna

The trigger grabs the max value from t and stores it in a table variable. The cte works out the row number of each insert to t and in the update phase adds it to the value stored int the table variable.
use sandbox
go
--drop table t
--create table t(workid int identity, requestid int,customfieldvalue int)
--go
IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[tGenCustomid]'))
drop trigger tGenCustomid
go
CREATE TRIGGER tGenCustomid
ON t
after insert
AS
BEGIN
DECLARE #max TABLE (
workid int,
requestid int,
customfieldvalue int
);
INSERT INTO #max (requestid,customfieldvalue)
select requestid,
coalesce(max(customfieldvalue),0)
from t
group by requestid
;with cte as
(select i.workid,i.requestid, row_number() over (partition by i.requestid order by i.workid) rn,
m.customfieldvalue
from inserted i
join #max m on m.requestid = i.requestid
)
update t
set customfieldvalue = cte.rn + cte.customfieldvalue
from t
join cte on cte.workid = t.workid and cte.requestid = t.requestid
END;
go
SET NOCOUNT ON
truncate table debug_table
truncate table t
print 'First bunch of inserts'
insert into t(requestid, customfieldvalue)
values
(1,0),(1,0),
(2,0),(2,0),(2,0),
(3,0)
select * from t
print 'Second bunch of inserts'
insert into t(requestid, customfieldvalue)
values
(1,0),(1,0)
select * from t
print 'Third bunch of inserts'
insert into t(requestid, customfieldvalue)
values
(1,0),(4,0),(3,0)
select * from t
First bunch of inserts
workid requestid customfieldvalue
----------- ----------- ----------------
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
6 3 1
Second bunch of inserts
workid requestid customfieldvalue
----------- ----------- ----------------
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
6 3 1
7 1 3
8 1 4
Third bunch of inserts
workid requestid customfieldvalue
----------- ----------- ----------------
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
6 3 1
7 1 3
8 1 4
9 1 5
10 4 1
11 3 2

Related

Find the level segments up-to 7-levels in parent-child hierarchy

Parent-child table
id
introducer_id
name
1
NULL
Riya
2
1
Ramesh
3
1
Anand
4
2
Preety
5
3
Rakesh
Query to get members list when id = 1
select id AS memberid, name
from (select *
from table_member
order by introducer_id, id) table_member_sorted,
(select #pv := '1') initialisation
where find_in_set(introducer_id, #pv)
and length(#pv := concat(#pv, ',', id))
The above query displays output
member_id
name
2
Ramesh
3
Anand
4
Preety
5
Rakesh
Now i also want to find the level of all the members
(i.e) i want the output as:
member_id
name
level
2
Ramesh
1
3
Anand
1
4
Preety
2
5
Rakesh
2
How can i update my previous query to get the above output?
Note: Query to display maximum level upto 7
Thank You.
CREATE PROCEDURE get_tree (start_from INT)
BEGIN
DROP TABLE IF EXISTS tmp;
CREATE TABLE tmp (member_id INT PRIMARY KEY,
name VARCHAR(255),
level INT);
INSERT INTO tmp SELECT id, name, 0
FROM test
WHERE id = start_from;
REPEAT
INSERT IGNORE INTO tmp SELECT test.id, test.name, tmp.level + 1
FROM test
JOIN tmp ON tmp.member_id = test.introducer_id;
UNTIL !ROW_COUNT() END REPEAT;
SELECT * FROM tmp;
END
fiddle
I have shared screenshot link of my sql version. snipboard.io/qbdB0A.jpg – AfreenB
Your SQL server version is MariaDB 10.4.14. – Akina
For your server version use
WITH RECURSIVE
cte AS ( SELECT id member_id, name, 0 level
FROM test
WHERE id = #start_from
UNION ALL
SELECT test.id, test.name, cte.level + 1
FROM test
JOIN cte ON cte.member_id = test.introducer_id
-- WHERE cte.level < 7 )
SELECT *
FROM cte;
fiddle

update a field group by another having max of another field - mysql

ID GRP VAL CHK
--- ----- ----- ----
1 1 1 0
2 1 3 0
3 2 7 0
4 2 2 0
5 2 1 0
6 3 5 0
I want to set my CHK field to '1' having maximum value VAL for every group of GRP,
so ID 2,3,6 should be set.
I don't write my trials here, all seems rubbish :)
In MySQL, you can do this using the update/join syntax:
update table t join
(select grp, max(val) as maxval
from table t
group by grp
) tmax
on t.grp = tmax.grp and t.val = tmax.maxval
set t.chk = 1;

How to compare and change values in MYSQL

My table looks like:
[Number] [Value1]
1234567 8
1234567C 7
9876543 1
9876543C 2
5555555 3
5555555C 3
I want to search the entries for same values in the first column (except the "C" in the end of the number) and set the higher value in the second column to the lower one.
There are always only two same values (one with "C") and some pairs have same values in the second column and some have different.
The result of the query should be:
Number Value1
1234567 7
1234567C 7
9876543 1
9876543C 1
5555555 3
5555555C 3
The following is not an ideal solution but should do what you want:
update yourTable
set value1 = (
select min(value1) from (
select * from yourTable
) as x
where yourTable.number = x.number + 'C');
I have tested it with this in mysql workbench:
create table yourTable(number varchar (10),value1 int);
insert into yourTable Values('1234567',8);
insert into yourTable Values('1234567C',7);
insert into yourTable Values('9876543',1);
insert into yourTable Values('9876543C',2);
insert into yourTable Values('5555555',3);
insert into yourTable Values('5555555C',3);
insert into yourTable Values('55555556',10);
insert into yourTable Values('55555556C',2);
Then select * from yourTable;will return:
1234567 8
1234567C 7
9876543 1
9876543C 2
5555555 3
5555555C 3
55555556 10
55555556C 2
After the update select * from yourTable; will return:
1234567 7
1234567C 7
9876543 1
9876543C 1
5555555 3
5555555C 3
55555556 2
55555556C 2
Hope that is what you wanted :)
Actually, you don't need any checking, since there are only 2 values (and thus the query is even simpler):
UPDATE
table
SET
Value1 =
(
SELECT
MAX(Value1)
FROM
table t
WHERE
table.Number = t.Number
OR table.Number = t.Number + 'C'
)
WHERE
RIGHT(Number, 1) != 'C'

auto increment for each foreignKey

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?

Selecting rows with reference id's to same table from mysql

I have a table such as:
id name ref_id order data_obj
-- ---- ------ ----- --------
1 Sam 0 15 [binary data]
2 Jack 0 20 [binary data]
3 Sue 0 25 [binary data]
4 Sam2 1 - [no data]
5 Sue2 3 - [no data]
6 Sam3 1 - [no data]
The idea is that I have more columns other than data_obj which can be common, so I don't want to insert them again, just want to insert a reference id to the same data.
Is it possible to write a query and select this:
1 - Sam - binary data from id 1
4 - Sam2 - binary data from id 1
6 - Sam3 - binary data from id 1
2 - Jack - binary data from id 2
3 - Sue - binary data from id 3
5 - Sue2 - binary data from id 3
Please note that I'm ordering according to column named order and there's no actual data for this column for referenced rows.
SELECT t1.id, t1.name, t2.data_obj
FROM your_table t1
LEFT JOIN your_table t2 ON t1.ref_id = t2.id
ORDER BY t1.order
Other version, which doesn't return rows without ref
SELECT t1.id, t1.name, t2.data_obj
FROM your_table t1, your_table t2
WHERE t1.ref_id = t2.id
ORDER BY t1.order
Here's a modification of #vartec's answer. This modification uses COALESCE() to combine the data_obj from either the primary row or the referenced row.
SELECT t1.id, t1.name, COALESCE(t1.data_obj, t2.data_obj)
FROM your_table t1
LEFT JOIN your_table t2 ON t1.ref_id = t2.id
ORDER BY COALESCE(t1.order, t2.order), ref_id;
COALESCE() is a standard SQL function that returns its first non-NULL argument.
Why aren't you using more than one table?
CREATE TABLE user (
user_id number not null (some form of auto increment or sequence),
name varchar(50) not null,
otherdata type,
primary key (id));
CREATE TABLE common (
common_id number not null (autoinc),
user_id number not null,
commondata type,
primary key (common_id),
unique index (user_id, common_id));
SELECT u.name, u.otherdata, c.commondata
FROM user u, common c
WHERE u.user_id = c.user_id
TABLE user
user_id name otherdata
1 Sam abc
2 Jack def
3 Sue ghi
Table common
common_id user_id commondata
1 1 AAA
2 1 BBB
3 1 CCC
4 2 DDD
5 3 EEE
6 3 FFF
Output
name otherdata commondata
Sam abc AAA
Sam abc BBB
Sam abc CCC
Jack def DDD
Sue ghi EEE
Sue ghi FFF