MySQL, Remove duplicate - mysql

I need to remove duplicates from my table but MySQL is not working properly
Create table emp
( empID INT(5) PRIMARY KEY,
Pref01 int(1),
Pref02 int(1),
Pref03 int(1),
Pref04 int(1))
empID, Pref01, Pref02, Pref03, Pref04
=====================================
00011 1 2 0 0
00011 1 3 0 0
00022 1 1 0 0
00022 0 3 0 0
I need to keep these records
00011 1 3 0 0
00022 0 3 0 0
also I need to keep any record with all pref null value
this is my sql:
select empID
FROM emp
where max(Pref01) or max (Pref02) or max(Pref03) or max(Pref04)
or Pref01 is null or Pref02 is null or Pref03 is null or Pref04 is null

Your problem is quite complicated, and with given information I have to make an assumption to answer it..
Assume there is no record with same empID have same maximal pref number...
SELECT A.*
FROM emp AS A
INNER JOIN (
SELECT empID, MAX(GREATEST(Pref01, Pref02, Pref03, Pref04)) AS MaxPref
FROM emp GROUP BY empID
) AS B ON A.empID = B.empID
WHERE
(Pref01 = MaxPref OR Pref02 = MaxPref OR Pref03 = MaxPref OR Pref04 = MaxPref)
OR
(Pref01 IS NULL AND Pref02 IS NULL AND Pref03 IS NULL AND Pref04 IS NULL)
If the assumption is not correct then the code will still show duplicate for empID with same max pref number more than one.. to fix it is much more complicated than this code..

You can get your data using GROUP BY and GREATEST:
SELECT empID, max(GREATEST(Pref01, Pref02, Pref03, Pref04)) FROM emp GROUP BY empID
you can use this to find rows directly in emp table using exists or in

Related

mysql with join in mysql

**i have a 3 tables in mysql **
1- Client Table
PK
NUM_CLIENT
NAME_CLIENT
1
ALEX
2
ADAM
2- Invoice Table
FK
NUM_CLIENT_INV
INV_VALUE
1
1000
1
2000
3- Sales returns Table
FK
NUM_CLIENT_SR
Return_VALUE
1
300
1
400
my query is
SELECT NAME_CLIENT , NUM_CLIENT , INV_VALUE , Return_VALUE FROM client
LEFT JOIN Invoice
ON NUM_CLIENT_INV = NUM_CLIENT
LEFT JOIN Sales returns
ON NUM_CLIENT_SR = NUM_CLIENT
The expected result is :
NAME_CLIENT
NUM_CLIENT
INV_VALUE
Return_VALUE
ALEX
1
1000
null
ALEX
1
null
300
ALEX
1
2000
null
ALEX
1
null
400
ADAM
2
null
null
but the result was given is :
NAME_CLIENT
NUM_CLIENT
INV_VALUE
Return_VALUE
ALEX
1
1000
300
ALEX
1
1000
400
ALEX
1
2000
300
ALEX
1
2000
400
ADAM
2
null
null
there is duplicate in result in columns INV_VALUE and Return_VALUE
What Is The Right Query That Give Me The expected result ?
Your existing joins return all combinations of client, optional invoice, and optional sales rows for each client.
If you want invoices and sales on separate rows, you need to join them in separate queries and union those queries. Use an inner joins so that if there are neither sales nor invoices you don't get two null/null rows, but then you have to add an extra query to get the null/null row when there are neither.
SELECT NAME_CLIENT , NUM_CLIENT , INV_VALUE , NULL as Return_VALUE FROM client
INNER JOIN Invoice
ON NUM_CLIENT_INV = NUM_CLIENT
UNION ALL
SELECT NAME_CLIENT , NUM_CLIENT , NULL , Return_VALUE FROM client
INNER JOIN Sales returns
ON NUM_CLIENT_SR = NUM_CLIENT
UNION ALL
SELECT NAME_CLIENT , NUM_CLIENT , NULL , NULL FROM client
LEFT JOIN Invoice
ON NUM_CLIENT_INV = NUM_CLIENT
LEFT JOIN Sales returns
ON NUM_CLIENT_SR = NUM_CLIENT
WHERE NUM_CLIENT_INV IS NULL AND NUM_CLIENT_SR IS NULL
ORDER BY NUM_CLIENT

How to select data in mysql that all the joined table is not null on certain column

i have 2 table which is one to many
table order
order_id
order_date
1
2021/01/01
2
2021/01/02
3
2021/01/02
table detail order
detail_order_id
order_id
is_finished
1
1
null
2
1
2021/01/03
3
2
2021/01/04
4
2
2021/01/04
5
3
2021/01/05
6
3
2021/01/06
7
3
null
so i wanna data that have condition if some of the detail order rows is_finished column not null, so the status is gonna be not finish.
and if all the detail order rows is_finished column not contain any null value like id 2, so the status is finished
expected result
order_id
status
1
not finish
2
finished
3
not finish
It seems like you don't really need a join since table_detail_order already have order_id and you only want to check is_finished, you might just need a query on 1 table like:
SELECT order_id,
CASE WHEN SUM(is_finished IS NULL)=0
THEN 'Finished' ELSE 'Not finish' END AS 'Status'
FROM table_detail_order GROUP BY order_id;
Demo fiddle
Btw, is_finished IS NULL will return 1 (true) or 0 (false) so in a table it would look like:
order_id
is_finished
is_finished IS NULL
1
null
1
1
2021/01/03
0
2
2021/01/04
0
2
2021/01/04
0
3
2021/01/05
0
3
2021/01/06
0
3
null
1
Therefore SUM(is_finished IS NULL) with GROUP BY order_id; will do something like this:
order_id
SUM(is_finished IS NULL)
1
1+0=1
2
0+0=0
3
0+0+1=1
And that is why CASE WHEN SUM(is_finished IS NULL)=0 ... is considered as finished while otherwise as not finish.
we can solve the problem like this
left join order and order_detail,but has condition order_details.is_finished is null
so we get a result that the joined order_details's is_finished only null
in that case there is no order_details join with order 2
then we regard the result as a temp table,so when joined order_details has data then it is not finished
here is the example data,you can run query in it
select id,
case when order_id>0 then 'not finish' else 'finished' end as status
from (
select o.id,od.order_id from `order` as o
left join order_detail as od
on (o.id=od.order_id and od.is_finished is null)
group by o.id
) as _t
You can try this. This query uses a LEFT JOIN and COUNT. Where the first count counted the NULL values as ZERO and the second count counts all values, then compare the 2 counts, if the first and second count is equal to each other it means that the order details is finished, if not then not finish.
SELECT a.`order_id`,
IF(COUNT(IF(ISNULL(is_finished),0,1))=COUNT(is_finished), 'finished', 'not finish') AS `status` FROM `order` a
LEFT JOIN `detail_order` b ON a.`order_id`=b.`order_id`
GROUP BY a.`order_id` ;
RESULT*
order_id status
-------- ------------
1 not finish
2 finished
3 not finish

Merge the query results,optimize?

mysql,two tables:test(one) and review(many).
My goal:from review for one in the corresponding number
SELECT t.ID,t.TITLE,COALESCE(COUNT(r.ID),0) `count`
FROM `test` t
LEFT OUTER JOIN review r
ON t.ID = r.REVIEW_OBJ_ID
WHERE r.REVIEW_TYPE = '4'
ORDER BY `count` DESC;
Output:
ID TITLE count
402884f657e0a6d20157e0a82cc90000 brother 2
test table(A small portion of the data)
SELECT t.ID,t.TITLE
FROM `test` t;
Output:
ID TITLE
40284c8157ad8e7d0157ad8f86880000 1234567890123456789012345
402884f657e0a6d20157e0a82cc90000 brother
402884f657e0a6d20157e11967a20036 fg
402884f657e51eff0157e54cd8610004 AAA
402884f657e652fb0157e65642750000 BBB
0000000057f4dc900157f4ea9edd0000 VVV
00000000580065c5015800746d750000 CCC
00000000580065c501581d9f04f0000b TTT
And I want get this:
ID TITLE count
402884f657e0a6d20157e0a82cc90000 brother 2
402884f657e652fb0157e65642750000 BBB 0
00000000580065c501581d9f04f0000b TTT 0
402884f657e0a6d20157e11967a20036 fg 0
0000000057f4dc900157f4ea9edd0000 VVV 0
40284c8157ad8e7d0157ad8f86880000 1234567890123456789012345 0
402884f657e51eff0157e54cd8610004 AAA 0
00000000580065c5015800746d750000 CCC 0
so,I tried this and it worked:
SELECT t.ID,t.TITLE, COALESCE(r.c,0) `count`
FROM `test` t
LEFT OUTER JOIN
(
SELECT r.REVIEW_OBJ_ID obj_id, COUNT(r.ID) c
FROM review r,`test` t
WHERE r.REVIEW_TYPE = '4'
AND t.ID = r.REVIEW_OBJ_ID
) r ON r.obj_id = t.ID
ORDER BY `count` DESC;
But I have two questions:
It feels I can use one-time select to found out result,but I use two-times select.Can I optimize it?
Add a count(redundant) in the test table fields, whether it is a better choice.
/REVIEW_TYPE and REVIEW_OBJ_ID decide which object is reviewed,just like I use "REVIEW_TYPE='4'" to contact the test table/
drop table if exists user_doctor_review;
create table review
(
ID varchar(64) not null,
USER_ID varchar(64),
DOCTOR_ID varchar(64),
REVIEW_TYPE varchar(1),
REVIEW_OBJ_ID varchar(64),
SERVICE_SCORE int(6),
REVIEW_CONTENT varchar(600),
REVIEW_TIME datetime,
POID varchar(64),
IS_ANONYMITY varchar(1),
CHECKED_STATUS varchar(1),
STATUS varchar(1),
REPLY_CONTENT varchar(600),
REPLY_TIME datetime,
DOCTOR_IS_READ varchar(1),
primary key (ID)
);
Yes you can do it with one SELECT statement with this query
SELECT test.ID, test.TITLE, count(review.ID) as count from test
left join review on test.ID = review.REVIEW_OBJ_ID
where review.REVIEW_TYPE = 4 or review.ID is null
group by test.ID
I have created a SQL Fiddle here: http://sqlfiddle.com/#!9/c6a178/15
Explanation:
The key points are:
or review.ID is null
because it will make the query list the tests with no reviews, and
group by test.ID
this will get the correct review counts related to the test.
Results:
ID TITLE count
-------------------------------- ------------------------- -----
0000000057f4dc900157f4ea9edd0000 VVV 0
00000000580065c5015800746d750000 CCC 0
00000000580065c501581d9f04f0000b TTT 1
40284c8157ad8e7d0157ad8f86880000 1234567890123456789012345 0
402884f657e0a6d20157e0a82cc90000 brother 2
402884f657e0a6d20157e11967a20036 fg 0
402884f657e51eff0157e54cd8610004 AAA 0
402884f657e652fb0157e65642750000 BBB 0

Validating presence of value(s) in a (sub)table and return a "boolean" result

I want to create a query in MySQL, on an order table and verify if it has a booking id, if it does not have a booking_id it should available on all relations in the invoice table.
I want the value returned to be a boolean in a single field.
Taken the example given, in
Case of id #1 I expect an immediate true, because it's available
Case of id #2 I expect an "delayed" false from the invoice table as not all related invoices have an booking_id, it should only return true if invoice id #3 actually has an booking id, meaning all invoices have an booking_id when the order does not.
I've tried several ways but still failed and don't even know what the best way to tackle this is.
Thanks for your input in advance!
Table order:
|----+------------+
| id | booking_id |
|----+------------+
| 1 | 123 |
| 2 | NULL |
|----+------------+
Table invoice:
+----+----------+------------+
| id | order_id | booking_id |
+----+----------+------------+
| 1 | 1 | 123 |
| 2 | 2 | 124 |
| 3 | 2 | NULL |
+----+----------+------------+
Schema
CREATE TABLE IF NOT EXISTS `invoice` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) NOT NULL,
`booking_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
)
CREATE TABLE IF NOT EXISTS `order` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`booking_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
If I understand you correctly, this is the base query for your request:
SELECT
O.id
, SUM(CASE WHEN I.booking_id IS NOT NULL THEN 1 ELSE 0 END) AS booked_count
, COUNT(1) AS total_count
, CASE WHEN SUM(CASE WHEN I.booking_id IS NOT NULL THEN 1 ELSE 0 END) = COUNT(1) THEN 1 ELSE 0 END AS has_all_bookings
FROM
`order` O
LEFT JOIN invoice I
ON O.id = I.order_id
GROUP BY
O.id
If you want to check if there is no record in the invoice table add the COUNT(1) to the last CASE statement as an additional condition (COUNT(1) = 0)
Fiddle Demo
I have not understood how the logic works out when the order is booked but some of the invoices are not. I'll presume either is good for a true value (OR logic). I'd avoid COUNT and GROUP BY and go for a SUBSELECT, which works fine in MySQL (I'm using and old 5.1.73-1 version).
This query gives you both values in distinct columns:
SELECT o.*
, (booking_id IS NOT NULL) AS order_booked
, (NOT EXISTS (SELECT id FROM `invoice` WHERE order_id=o.id AND booking_id IS NULL)) AS invoices_all_booked
FROM `order` o
Of course you can combine the values:
SELECT o.*
, (booking_id IS NOT NULL OR NOT EXISTS (SELECT id FROM `invoice` WHERE order_id=o.id AND booking_id IS NULL)) AS booked
FROM `order` o
Here you go, create a view that does it
create view booked_view as
select `order`.id as order_id
,
case when booking_id > 0 then true
when exists (SELECT id FROM invoice WHERE order_id=`order`.id AND invoice.booking_id IS NULL) then true
else false
end as booked
from `order` ;
Then just join your view to the order table and you will have your boolean column 'booked'
select o.id, booked from `order` o
join booked_view on (o.id = booked_view.order_id)

MySQL merge amounts in a 2 rows

I'm looking to create a sql statement that will update a large set of data.
What I have is a table like
id, transid, amount, narative1, narative 2, total, active
1 1234 23.2 NULL NULL NULL 1
2 1234 120.33 NULL NULL NULL 1
3 1235 98.00 NULL NULL NULL 1
When there are two rows with the same transid I need to total them put the result in the total column of the first one with that transid and put the second amount in naritive2 of the first instance as well as make the second one inactive. It should ignore single rows for a transid.
The result of what I want to do should be:
id, transid, amount, narative1, narative 2, total, active
1 1234 23.2 NULL 120.33 143.53 1
2 1234 120.33 NULL NULL NULL 0
3 1235 98.00 NULL NULL NULL 1
I know a bit of a thong twister but..
Ideally I'd like to do this in just a MySQL statements. So I don't mind having to do multiple sql statements but I want to avoid connecting it to PHP etc. Its a very large set of data.
This will update only those transactions that have exactly 2 rows (not 1 and not 3 or more).
UPDATE mytable mtu
JOIN (
SELECT minid, maxid, mtmin.amount AS minamt, mtmax.amount AS maxamt
FROM (
SELECT MIN(id) AS minid, MAX(id) AS maxid
FROM mytable mti
GROUP BY
transid
HAVING COUNT(*) = 2
) mt
JOIN mytable mtmin
ON mtmin.id = minid
JOIN mytable mtmax
ON mtmax.id = maxid
) mts
ON id IN (minid, maxid)
SET narative2 = CASE id WHEN minid THEN minamt ELSE NULL END,
total = CASE id WHEN minid THEN minamt + maxamt ELSE NULL END,
active = (id = minid)