Sort the table by c_criticality field in sql query - mysql

i have one field wonum,C_criticality in workorder.
In C_criticality field have three option
critical
non-critical
tools
4.null or empty
In that C_criticality field some columns are null(empty) that should come at last.
Now i have to get the output in sorting order by criticality , noncritical ,tool , null value(empty value ) will come.
CREATE TABLE workorder
(
wonum int,
C_criticality varchar(255),
);
INSERT INTO workorder (wonum,C_criticality)
VALUES (2, 'critical');
INSERT INTO workorder (wonum,C_criticality)
VALUES (1, 'non-critical');
INSERT INTO workorder (wonum,C_criticality)
VALUES (15, 'critical');
INSERT INTO workorder (wonum,C_criticality)
VALUES (12, 'tool');
INSERT INTO workorder (wonum,C_criticality)
VALUES (21, 'non-critical');
INSERT INTO workorder (wonum,C_criticality)
VALUES (11, '');
output:-
C_criticality wonum
critical 2
critical 15
non-critical 21
tool 12
null 11

We can try a two-tiered sort using ISNULL and FIELD:
SELECT *
FROM yourTable
ORDER BY
ISNULL(C_criticality), -- 0 for non NULL, 1 for NULL
FIELD(C_criticality, 'criticality', 'noncritical', 'tool');
The call to FIELD will order according to the list you provided.

This works for all SQL engines, not just in MySQL
select *
from workorder
order by case when C_criticality = 'critical' then 1
when C_criticality = 'non-critical' then 2
when C_criticality = 'tools' then 3
else 4
end

Since you have tagged mysql the query would be something like ,
SELECT * FROM workorder ORDER BY FIELD(C_criticality ,'critical','non-critical','tools','null');

It depends from DBMS. In some there are options NULLS FIRST, NULLS LAST
In MySQL you can find this article interesting https://www.designcise.com/web/tutorial/how-to-order-null-values-first-or-last-in-mysql

Related

MySQL Show Truncate and SUM NULL values

If the Cost column is showing NULL how do I ignore this function and simply show NULL?
Currently it is omitting the results when I added this function. I tried position the COALESCE function with no luck.
Note: Without this select statement it shows all the entries so the JOINS are correct and the results are particular to this function.
Thanks in advance!
TRUNCATE(SUM(Cost/2),3) AS CostPerPart
Please feel free to provide more information but this may work depending on your structure and data. This relies on the use of IFNULL() The documentation is located here.
IFNULL will return the expression if the value is not NULL otherwise it will return NULL
IFNULL(expr1,expr2)
If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.
CREATE TABLE Table1
(`id` int, `Cost` int)
;
INSERT INTO Table1
(`id`, `Cost`)
VALUES
(1, 2),
(2, 3),
(3, 4.5),
(4, 6),
(5, NULL),
(1, 2)
;
SELECT id
,IFNULL(TRUNCATE(SUM(Cost/2),3), NULL) AS CostPerPart
FROM Table1
GROUP BY id;
Output:
id CostPerPart
1 2
2 1.5
3 2.5
4 3
5 (null)
SQLFiddle

insert using Triggers in mysql [duplicate]

I want to make a insert into 2 tables
visits:
visit_id int | card_id int
registration:
registration_id int | type enum('in','out') | timestamp int | visit_id int
I want something like:
INSERT INTO `visits` as v ,`registration` as v
(v.`visit_id`,v.`card_id`,r.`registration_id`, r.`type`, r.`timestamp`, r.`visit_id`)
VALUES (NULL, 12131141,NULL, UNIX_TIMESTAMP(), v.`visit_id`);
I wonder if its possible
It's not possible with one query as INSERT can only insert data to one table in mysql. You can either
write this as two queries and execute them as a batch
create a stored procedure that would execute two insert command
You can wrap those inserts in transaction if you need to make sure that both queries will write the data.
It seems like the problem you are trying to solve is to get the auto-increment value from the "visits" row to insert into "registration". Am I right?
If so, you can just use the LAST_INSERT_ID() function like this:
INSERT INTO `visits` (`visit_id`,`card_id`)
VALUES (NULL, 12131141);
INSERT INTO `registration` (`registration_id`, `type`, `timestamp`, `visit_id`)
VALUES (NULL, 'in', UNIX_TIMESTAMP(), LAST_INSERT_ID());
You can always do something like this
INSERT IGNORE INTO `table2` VALUES ((select id from table1 where col="value"), 3, 4, 5)
INSERT INTO designation as de,
department as da,
profile as pr
(designation_name,
depart_id,
id,
username,
department,
designation)
select de.designation_name,
de.depart_id,da.id,
pr.username,
pr.department,
pr.designation
from
designation,
department,
profile
de.designation_name='project manager' AND de.id='1' OR
de.depart_id='2' AND de.id='2' OR
da.id='2' OR
pr.username='kapil.purohit' AND pr.id='9' AND pr.status='1' OR
pr.department='1' AND pr.id='9' OR
pr.designation='3' AND pr.id='9' AND pr.status='1'
WHERE
de.id = da.id AND
da.id = pr.id AND
de.id = pr.id AND
ORDER BY de.id DESC

SQL Insert from Select to same table but with a changed ID - MySQL

I need to copy a number of rows from a table that have the same id_shop value, then insert these rows back into the same table but with a different id_shop value. I'm not sure how to do the later part. I'm guessing that it will be a variation of the following.
INSERT INTO `ps_hook_module`(`id_module`, `id_shop`, `id_hook`, `position`)
SELECT `id_module`, `id_shop`, `id_hook`, `position` FROM `ps_hook_module` WHERE
`id_shop` = 1
INSERT INTO `ps_hook_module`(`id_module`, `id_shop`, `id_hook`, `position`)
SELECT `id_module`, 42, `id_hook`, `position` FROM `ps_hook_module`
WHERE `id_shop` = 1
42 is a different id_shop value you wanted

auto_increment column for a group of rows?

I am trying to figure out how to do a table with 3 columns:
unique_id, type, version
Where unique_id is AUTO_INCREMENT for each record, and version is AUTO_INCREMENT for each type.
The purpose being, when I insert I only have to specify 'type' and the unique_id and version_id are automatically generated. eg:
insert type 'a', then values are: 1 , a , 1
insert type 'a', then values are: 2 , a , 2
insert type 'a', then values are: 3 , a , 3
insert type 'b', then values are: 4 , b , 1
insert type 'b', then values are: 5 , b , 2
insert type 'a', then values are: 6 , a , 4
Also would it be fair to say that such a setup is not really normalised? That instead it should be two tables?
You can't have auto-generated sequences for each type, but you can generate your own.
insert into the_table (type, version)
select 'a', 1 + IFNULL(max(version), 0)
from the_table
where type = 'a';
If the table will have a lot of rows (and depending on what you're trying to do), then it would be best to have separate tables for each version. Then, insert each type into its proper version-table.

Mysql insert into 2 tables

I want to make a insert into 2 tables
visits:
visit_id int | card_id int
registration:
registration_id int | type enum('in','out') | timestamp int | visit_id int
I want something like:
INSERT INTO `visits` as v ,`registration` as v
(v.`visit_id`,v.`card_id`,r.`registration_id`, r.`type`, r.`timestamp`, r.`visit_id`)
VALUES (NULL, 12131141,NULL, UNIX_TIMESTAMP(), v.`visit_id`);
I wonder if its possible
It's not possible with one query as INSERT can only insert data to one table in mysql. You can either
write this as two queries and execute them as a batch
create a stored procedure that would execute two insert command
You can wrap those inserts in transaction if you need to make sure that both queries will write the data.
It seems like the problem you are trying to solve is to get the auto-increment value from the "visits" row to insert into "registration". Am I right?
If so, you can just use the LAST_INSERT_ID() function like this:
INSERT INTO `visits` (`visit_id`,`card_id`)
VALUES (NULL, 12131141);
INSERT INTO `registration` (`registration_id`, `type`, `timestamp`, `visit_id`)
VALUES (NULL, 'in', UNIX_TIMESTAMP(), LAST_INSERT_ID());
You can always do something like this
INSERT IGNORE INTO `table2` VALUES ((select id from table1 where col="value"), 3, 4, 5)
INSERT INTO designation as de,
department as da,
profile as pr
(designation_name,
depart_id,
id,
username,
department,
designation)
select de.designation_name,
de.depart_id,da.id,
pr.username,
pr.department,
pr.designation
from
designation,
department,
profile
de.designation_name='project manager' AND de.id='1' OR
de.depart_id='2' AND de.id='2' OR
da.id='2' OR
pr.username='kapil.purohit' AND pr.id='9' AND pr.status='1' OR
pr.department='1' AND pr.id='9' OR
pr.designation='3' AND pr.id='9' AND pr.status='1'
WHERE
de.id = da.id AND
da.id = pr.id AND
de.id = pr.id AND
ORDER BY de.id DESC