Create trigger for several rows - mysql

I have table users AND orders. After every UPDATE row in orders. I want update DATA in users table namely concat(OLD.DATA + ID which was updated).
Table 'users'.
ID NAME DATA
1 John 1|2
2 Michael 3|4
3 Someone 5
Table 'orders'.
ID USER CONTENT
1 1 ---
2 1 ---
3 2 ---
4 2 ---
5 3 ---
For example:
SELECT `data` from `users` where `id` = 2; // Result: 3|4
UPDATE `orders` SET '...' WHERE `id` > 0;
**NEXT LOOP**
UPDATE `users` SET `data` = concat(OLD.data, ID.rowUpdated) WHERE `user` = 1;
UPDATE `users` SET `data` = concat(OLD.data, ID.rowUpdated) WHERE `user` = 1;
UPDATE `users` SET `data` = concat(OLD.data, ID.rowUpdated) WHERE `user` = 2;
UPDATE `users` SET `data` = concat(OLD.data, ID.rowUpdated) WHERE `user` = 2;
UPDATE `users` SET `data` = concat(OLD.data, ID.rowUpdated) WHERE `user` = 3;
Result:
SELECT data from users where id = 1; // Result: 1|2|1|2
SELECT data from users where id = 2; // Result: 3|4|3|4
SELECT data from users where id = 3; // Result: 5|5
How can I do it?

I think you are making the same mistake I made not too long ago, ie storing an array/object in a column.
I would recommend using the following tables in your scenario:
users
+-----------+-----------+
| id | user_name |
+-----------+-----------+
| 1 | John |
+-----------+-----------+
| 2 | Michael |
+-----------+-----------+
orders
+-----------+-----------+------------+
| id | user_id |date_ordered|
+-----------+-----------+------------+
| 1 | 1 | 2019-03-05 |
+-----------+-----------+------------+
| 2 | 2 | 2019-03-05 |
+-----------+-----------+------------+
Where user_id is the foreign key to users
sales
+-----------+-----------+------------+------------+------------+
| id | order_id | item_sku | qty | price |
+-----------+-----------+------------+------------+------------+
| 1 | 1 | 1001 | 1 | 2.50 |
+-----------+-----------+------------+------------+------------+
| 2 | 1 | 1002 | 2 | 3.00 |
+-----------+-----------+------------+------------+------------+
| 3 | 2 | 1001 | 2 | 2.00 |
+-----------+-----------+------------+------------+------------+
where order_id is the foreign key to orders
Now for the confusing part. You will need to use a series of JOINs to access the relevant data for each user.
SELECT
t3.id AS user_id,
t3.user_name,
t1.id AS order_id,
t1.date_ordered,
SUM((t2.price * t2.qty)) AS order_total
FROM orders t1
JOIN sales t2 ON (t2.order_id = t1.id)
LEFT JOIN users t3 ON (t1.user_id = t3.id)
WHERE user_id=1
GROUP BY order_id;
This will return:
+-----------+--------------+------------+------------+--------------+
| user_id | user_name | order_id |date_ordered| order_total |
+-----------+--------------+------------+------------+--------------+
| 1 | John | 1 | 2019-03-05 | 8.50 |
+-----------+--------------+------------+------------+--------------+
These type of JOIN statements should come up in basically any project using a relational database (that is, if you are designing your DB correctly). Typically I create a view for each of these complicated queries, which can then be accessed with a simple SELECT * FROM orders_view
For example:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`#`localhost`
SQL SECURITY DEFINER
VIEW orders_view AS (
SELECT
t3.id AS user_id,
t3.user_name,
t1.id AS order_id,
t1.date_ordered,
SUM((t2.price * t2.qty)) AS order_total
FROM orders t1
JOIN sales t2 ON (t2.order_id = t1.id)
LEFT JOIN users t3 ON (t1.user_id = t3.id)
GROUP BY order_id
)
This can then be accessed by:
SELECT * FROM orders_view WHERE user_id=1;
Which would return the same results as the query above.
Depending on your needs, you will probably need to add a few more tables (addresses, products etc.) and several more rows to each of these tables. Very often you will find that you need to JOIN 5+ tables into a view, and sometimes you might need to JOIN the same table twice.
I hope this helps despite it not exactly answering your question!

It is probably a bad idea to update the USERS table after inserting into (or updating) the ORDERS table. Avoid storing data twice. In your case: you can always get all "order ids" for a user by querying the ORDERS table. Thus, you don't need to store them in the USERS table (again). Example (tested with MySQL 8.0, see dbfiddle):
Tables and data
create table users( id integer primary key, name varchar(30) ) ;
insert into users( id, name ) values
(1, 'John'),(2, 'Michael'),(3, 'Someone') ;
create table orders(
id integer primary key
, userid integer
, content varchar(3) references users (id)
);
insert into orders ( id, userid, content ) values
(101, 1, '---'),(102, 1, '---')
,(103, 2, '---'),(104, 2, '---'),(105, 3, '---') ;
Maybe a VIEW - similar to the one below - will do the trick. (Advantage: you don't need additional columns or tables.)
-- View
-- Inner SELECT: group order ids per user (table ORDERS).
-- Outer SELECT: fetch the user name (table USERS)
create or replace view userorders (
userid, username, userdata
)
as
select
U.id, U.name, O.orders_
from (
select
userid
, group_concat( id order by id separator '|' ) as orders_
from orders
group by userid
) O join users U on O.userid = U.id ;
Once the view is in place, you can just SELECT from it, and you will always get the current "userdata" eg
select * from userorders ;
-- result
userid username userdata
1 John 101|102
2 Michael 103|104
3 Someone 105
-- add some more orders
insert into orders ( id, userid, content ) values
(1000, 1, '***'),(4000, 1, '***'),(7000, 1, '***')
,(2000, 2, ':::'),(5000, 2, ':::'),(8000, 2, ':::')
,(3000, 3, '###'),(6000, 3, '###'),(9000, 3, '###') ;
select * from userorders ;
-- result
userid username userdata
1 John 101|102|1000|4000|7000
2 Michael 103|104|2000|5000|8000
3 Someone 105|3000|6000|9000

Related

How to get one extra record for LEFT JOIN to represent a record not include on the left joined table

I have a database with two tables one table (shops) has an admin user column and the other a user with less privileges. I plan to LEFT JOIN the table of the user with less privileges. When I retrieve the data, the records for the admin user must be on a separate row and must have NULL values for the left joined table followed by records of users with less privileges (records of the left joined table) if any. I am using MySQL.
I have looked into the UNION commands but I don't think it can help. Please see the results bellow of what I need.
Thank you.
SELECT *
FROM shops LEFT JOIN users USING(shop_id)
WHERE shop_id = 1 AND (admin_id = 1 OR user_id = 1);
+---------+----------+---------+
| shop_id | admin_id | user_id |
+---------+----------+---------+
| 1 | 1 | NULL | <-- Need this one extra record
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 3 |
+---------+----------+---------+
Here is an example structure of the databases and some sample data:
CREATE SCHEMA test DEFAULT CHARACTER SET utf8 ;
USE test;
CREATE TABLE admin(
admin_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(admin_id)
);
CREATE TABLE shops(
shop_id INT NOT NULL AUTO_INCREMENT,
admin_id INT NOT NULL,
PRIMARY KEY(shop_id),
CONSTRAINT fk_shop_admin FOREIGN KEY(admin_id) REFERENCES admin (admin_id)
);
CREATE TABLE users(
user_id INT NOT NULL AUTO_INCREMENT,
shop_id INT NOT NULL,
CONSTRAINT fk_user_shop FOREIGN KEY(shop_id) REFERENCES admin (shop_id)
);
-- Sample data
INSERT INTO admin() VALUES ();
INSERT INTO shops(admin_id) VALUES (1);
INSERT INTO users(shop_id) VALUES (1),(1),(1);
I think you need union all:
select s.shop_id, s.admin_id, null as user_id
from shops s
where s.shop_id = 1
union all
select s.shop_id, s.admin_id, u.user_id
from shops s join
users u
on s.shop_id = u.shop_id
where shop_id = 1;
Put your where condition in On clause
SELECT *
FROM shops LEFT JOIN users on shops.shop_id=users.shop_id and (admin_id = 1 OR user_id = 1)
WHERE shops.shop_id = 1

MySQL join tables and show values excluding specific user_id

Details:
MySQL tables
create table request (
id int not null primary key auto_increment,
date_requested timestamp,
user_id int
);
create table verify (
id int not null primary key auto_increment,
user_id int,
request_id int,
created timestamp
);
Values
Request Table
ID DATE REQUESTED USER ID
1 2020.. 1
2 2020.. 3
Verify Table
ID USER_ID REQUEST_ID CREATED
1 2 1 2020...
Problem:
I have this query below:
select * from request join verify on request.id = verify.request_id where verify.user_id !=2;
This returns me nothing. Why is it not showing the 2nd record from request table?
My goal is simply return all values from request tables based on this:
If record.id is in verify table then check if verify table has user_id of 2
If it does, then do not show that request record
If there are no record.id in verify table at all
Then show them
You need a left join of the tables and filter out the matching rows:
select r.*
from request r left join verify v
on r.id = v.request_id and v.user_id =2
where v.request_id is null
See the demo.
Or with NOT EXISTS:
select r.*
from request r
where not exists (
select 1 from verify v
where r.id = v.request_id and v.user_id =2
)
See the demo.
Results:
| id | date_requested | user_id |
| --- | ------------------- | ------- |
| 2 | 2020-04-08 00:00:00 | 3 |

SQL query relating how many of each collection set a given consumer has

Say I have customers who can be awarded certain prizes:
SELECT gs.claimed_by AS consumer_id, p.prize_id AS prize_id FROM
awarded_prizes
And right now, customer 1 has three prizes and customer 2 has a single prize
+-------------+----------+
| consumer_id | prize_id |
+-------------+----------+
| 1 | 45 |
| 1 | 46 |
| 1 | 47 |
| 2 | 66 |
+-------------+----------+
Say we also have collections, and if you collect all the members to that collectible, you now have a collectable set:
SELECT set_id, member_prize_id AS prize_id FROM collectable_set_members;
+--------+----------+
| set_id | prize_id |
+--------+----------+
| 1 | 45 |
| 1 | 46 |
| 1 | 47 |
| 2 | 65 |
| 2 | 66 |
+--------+----------+
With the above table and the previous query, we can see customer 1 has completed set 1 once (they have 45, 46, 47) and customer 2 has completed nothing.
There are cases where a customer can complete a set multiple times (customer could have 45, 46, 47, 45, 46, 47 in the awarded_prize table.
I've been looking at the pantry problem and its variations (like the bartender problem), have been playing with cross joins and groupings and can't seem to find what I want.
I'm trying to get a result for a given customer, showing all the set_ids they own and the number of sets they've completed:
+-------------+---------------+--------+
| consumer_id | completed_set | count |
+-------------+---------------+--------+
| 1 | 1 | 1 |
+-------------+---------------+--------+
I'm on mariadb:5.5
See here SqlFiddle
My tables have different names than yours, but it proves the point:
select sets_x_consumers.consumer_id, sets_x_consumers.set_id,
set_summary.items_in_set = consumer_summary.items_per_set_per_consumer as set_is_complete
from (
-- build a cross-product of sets and consumers
select distinct set_id, consumer_id
from sets join consumers -- no join condition -> cross product
) sets_x_consumers
inner join
( -- the total number of items in each set per set_id
select set_id, count(*) items_in_set
from sets
group by set_id
) set_summary on sets_x_consumers.set_id = set_summary.set_id
inner join
( -- the total number of items per set and customer
select set_id, consumer_id, count(*) items_per_set_per_consumer
from sets
inner join consumers on sets.prize_id = consumers.prize_id
group by consumer_id, set_id
) consumer_summary on sets_x_consumers.set_id = consumer_summary.set_id and sets_x_consumers.consumer_id = consumer_summary.set_id
My basic idea is to sum up the number of items in each set and the number of items per set each consumer has claimed. As long as there are no duplicate entries for the pair of consumer and prize, this should work (if duplicates were allowed, I would use count distinct(prize_id) for the consumer_summary).
The output of the query above is:
| consumer_id | set_id | set_is_complete |
|-------------|--------|-----------------|
| 1 | 1 | 1 |
| 2 | 2 | 0 |
This lists each pair of consumers and set for which a consumer has at least one prize. (to change this to list every consumer-set combination, use outer join)
Listing only complete sets or summarizing the number of complete sets should be easy on this basis ;-)
Can't really figure out what your last column 'count' is supposed to mean,
but here is a solution that lists users and their sets completed.
demo Link
The whole idea is to count the number of prizes required for each set, and count the collected prizes per customer per set, and thus you can join the two.
I know it's mssql, but I did not manage to make mysql ctes work in sqfiddle.
CTE-s is nothing more than a subquery basically. If your server does not support CTE-s you could use normal subqueries or temp tables instead.
For what it's worth I came up with a nice routine for this in Sql Server. This works even if there are overlapping prize_id values in each set (will default to higher setid if ambiguous). Assume all temp tables are original data:
declare #awarded_prize table (rowid int identity, consumer_id int, prize_id int )
insert #awarded_prize
select * from #awarded_prizes
declare #collections table ( set_id int, prize_id int, rownumber int , filled int)
insert #collections
select *, row_number() over(partition by set_id order by set_id, prize_id) , null
from #collections
declare #todelete table (rowid int)
declare #scorecard table (consumer_id int, set_id int)
declare #iterator int=1
declare #prize_id int
declare #set_id int = (Select min(set_id) from #collections)
declare #consumer_id int = (Select min(consumer_id) from #awarded_prize)
while #consumer_id<=(select max(consumer_id) from #awarded_prize)
begin
while #set_id<=(select max(set_id) from #collections)
begin
while 1=1
begin
select #prize_id=prize_id
from #collections
where set_id=#set_id and rownumber=#iterator
if (select max(rowid) from #awarded_prize where prize_id=#prize_id and consumer_id=#consumer_id and rowid not in (select rowid from #todelete)) is null break
insert #todelete
select max(rowid) from #awarded_prize where prize_id=#prize_id and consumer_id=#consumer_id and rowid not in (select rowid from #todelete)
update #collections set filled=1
where rownumber=#iterator and set_id=#set_id
if not exists(select 1 from #collections where set_id=#set_id and filled is null)
begin
insert #scorecard
select #consumer_id, #set_id
delete #awarded_prize where rowid in (Select rowid from #todelete)
delete #todelete
update #collections set filled=null where filled=1
end
set #iterator=case when #iterator=(Select max(rownumber) from #collections where set_id=#set_id) then
(select min(rownumber) from #collections where set_id=#set_id) else #iterator+1 end
end
delete #todelete
set #iterator=1
set #set_id=#set_id+1
end
set #iterator=1
select #set_id=min(set_id) from #collections
select #consumer_id=min(consumer_id) from #awarded_prize where consumer_id>#consumer_id
end
select consumer_id, set_id, count(*) complete_sets
from #scorecard
group by consumer_id, set_id
order by consumer_id, set_id

how to get last row from table 2 using left join mysql

I have tow tables tbl_product_checkout and tbl_product_checkout_status in which I want to get the last row from tbl_product_checkout_status
//tbl_product_checkout
product_checkout_id user_id product_checkout_order_no
-----------------------------------------------------------
1 1 ORD123456
//tbl_product_checkout_status
checkout_status_id product_checkout_id checkout_status_check
------------------------------------------------------------------
1 1 Dispatched
2 1 Delivered
I have tried using the following query
SELECT *
FROM tbl_product_checkout pc
LEFT
JOIN tbl_product_checkout_status cs
ON cs.product_checkout_id = pc.product_checkout_id
WHERE pc.user_id = 1
GROUP
BY pc.product_checkout_id
ORDER
BY cs.checkout_status_id DESC
but the output for above query is,
user_id product_checkout_order_no checkout_status_check
-------------------------------------------------------------
1 ORD123456 Dispatched
but I want the result as,
user_id product_checkout_order_no checkout_status_check
-------------------------------------------------------------
1 ORD123456 Delivered
Add a where = max sub query eg
DROP TABLE IF EXISTS tbl_product_checkout,tbl_product_checkout_status;
CREATE TABLE tbl_product_checkout(product_checkout_id INT, user_id INT, product_checkout_order_no VARCHAR(20));
INSERT INTO tbl_product_checkout VALUES
( 1 , 1 , 'ORD123456');
CREATE TABLE tbl_product_checkout_status(checkout_status_id INT, product_checkout_id INT, checkout_status_check VARCHAR(20));
INSERT INTO tbl_product_checkout_status VALUES
( 1 , 1 , 'Dispatched'),
( 2 , 1 , 'Delivered');
SELECT * FROM
tbl_product_checkout T1
LEFT JOIN tbl_product_checkout_status T2 ON T1.PRODUCT_CHECKOUT_ID = T2.PRODUCT_CHECKOUT_ID
WHERE T2.CHECKOUT_STATUS_ID = (
SELECT MAX(T3.CHECKOUT_STATUS_ID)
FROM tbl_product_checkout_status T3
WHERE T3.PRODUCT_CHECKOUT_ID = T2.PRODUCT_CHECKOUT_ID
)
;
Result
+---------------------+---------+---------------------------+--------------------+---------------------+-----------------------+
| product_checkout_id | user_id | product_checkout_order_no | checkout_status_id | product_checkout_id | checkout_status_check |
+---------------------+---------+---------------------------+--------------------+---------------------+-----------------------+
| 1 | 1 | ORD123456 | 2 | 1 | Delivered |
+---------------------+---------+---------------------------+--------------------+---------------------+-----------------------+
1 row in set (0.00 sec)
I think your group by mess up your desired outcome. I worked on your given database schema and cretaed a fiddle and managed to get your desired outcome. So your sql should be something like this:
SELECT * FROM tbl_product_checkout as pc
LEFT JOIN tbl_product_checkout_status as cs ON
cs.product_checkout_id = pc.product_checkout_id
WHERE pc.user_id = 1 ORDER BY cs.checkout_status_id DESC limit 1
By using limit 1, you will get last row as we ordered by DESC.
Keep in mind that i removed date part since there was no date on your example code.
Check Out Fiddle

Mysql unique values query

I have a table with name-value pairs and additional attribute. The same name can have more than one value. If that happens I want to return the row which has a higher attribute value.
Table:
ID | name | value | attribute
1 | set1 | 1 | 0
2 | set2 | 2 | 0
3 | set3 | 3 | 0
4 | set1 | 4 | 1
Desired results of query:
name | value
set2 | 2
set3 | 3
set1 | 4
What is the best performing sql query to get the desired results?
the best performing query would be as follows:
select
s.set_id,
s.name as set_name,
a.attrib_id,
a.name as attrib_name,
sav.value
from
sets s
inner join set_attribute_values sav on
sav.set_id = s.set_id and sav.attrib_id = s.max_attrib_id
inner join attributes a on sav.attrib_id = a.attrib_id
order by
s.set_id;
+--------+----------+-----------+-------------+-------+
| set_id | set_name | attrib_id | attrib_name | value |
+--------+----------+-----------+-------------+-------+
| 1 | set1 | 3 | attrib3 | 20 |
| 2 | set2 | 0 | attrib0 | 10 |
| 3 | set3 | 0 | attrib0 | 10 |
| 4 | set4 | 4 | attrib4 | 10 |
| 5 | set5 | 2 | attrib2 | 10 |
+--------+----------+-----------+-------------+-------+
obviously for this to work you're gonna also have to normalise your design and implement a simple trigger:
drop table if exists attributes;
create table attributes
(
attrib_id smallint unsigned not null primary key,
name varchar(255) unique not null
)
engine=innodb;
drop table if exists sets;
create table sets
(
set_id smallint unsigned not null auto_increment primary key,
name varchar(255) unique not null,
max_attrib_id smallint unsigned not null default 0,
key (max_attrib_id)
)
engine=innodb;
drop table if exists set_attribute_values;
create table set_attribute_values
(
set_id smallint unsigned not null,
attrib_id smallint unsigned not null,
value int unsigned not null default 0,
primary key (set_id, attrib_id)
)
engine=innodb;
delimiter #
create trigger set_attribute_values_before_ins_trig
before insert on set_attribute_values
for each row
begin
update sets set max_attrib_id = new.attrib_id
where set_id = new.set_id and max_attrib_id < new.attrib_id;
end#
delimiter ;
insert into attributes values (0,'attrib0'),(1,'attrib1'),(2,'attrib2'),(3,'attrib3'),(4,'attrib4');
insert into sets (name) values ('set1'),('set2'),('set3'),('set4'),('set5');
insert into set_attribute_values values
(1,0,10),(1,3,20),(1,1,30),
(2,0,10),
(3,0,10),
(4,4,10),(4,2,20),
(5,2,10);
This solution will probably perform the best:
Select ...
From Table As T
Left Join Table As T2
On T2.name = T.name
And T2.attribute > T1.attribute
Where T2.ID Is Null
Another solution which may not perform as well (you would need to evaluate against your data):
Select ...
From Table As T
Where Not Exists (
Select 1
From Table As T2
Where T2.name = T.name
And T2.attribute > T.attribute
)
select name,max(value)
from table
group by name
SELECT name, value
FROM (SELECT name, value, attribute
FROM table_name
ORDER BY attribute DESC) AS t
GROUP BY name;
There is no easy way to do this.
A similar question was asked here.
Edit: Here's a suggestion:
SELECT `name`,`value` FROM `mytable` ORDER BY `name`,`attribute` DESC
This isn't quite what you asked for, but it'll at least give you the higher attribute values first, and you can ignore the rest.
Edit again: Another suggestion:
If you know that value is a positive integer, you can do this. It's yucky, but it'll work.
SELECT `name`,CAST (GROUP_CONCAT(`value` ORDER by `attribute` DESC) as UNSIGNED) FROM `mytable` GROUP BY `name`
To include negative integers you could change UNSIGNED to SIGNED.
Might want to benchmark all these options, here's another one.
SELECT t1.name, t1.value
FROM temp t1
WHERE t1.attribute IN (
SELECT MAX(t2.attribute)
FROM temp t2
WHERE t2.name = t1.name);
How about:
SELECT ID, name, value, attribute
FROM table A
WHERE A.attribute = (SELECT MAX(B.attribute) FROM table B WHERE B.NAME = A.NAME);
Edit: Seems like someones said the same already.
Did not benchmark them, but here is how it is doable:
TableName = temm
1) Row with maximum value of attribute :
select t.name, t.value
from (
select name, max(attribute) as maxattr
from temm group by name
) as x inner join temm as t on t.name = x.name and t.attribute = x.maxattr;
2) Top N rows with maximum attribute value :
select name, value
from temm
where (
select count(*) from temm as n
where n.name = temm.name and n.attribute > temm.attribute
) < 1 ; /* 1 can be changed to 2,3,4 ..N to get N rows */