Mysql - Find duplicates records evaluating 2 columns - mysql

I have a mysql table with a structure like this:
order_id - customer_name - customer_email_address
i need to do a query to search for records that have same customer_name OR same customer_email and show the result ordered by order_id groups (descending order).
Example:
Mysql table
order_id customer_name customer_email_address
1 pippo pippo#pippo.com
2 pippo pippo2#pippo2.com
3 pluto pluto#pluto.com
4 caio pippo#pippo.com
5 pippo4 pippo4#pippo4.com
6 pluto pluto22#pluto22.com
Result
6 pluto pluto22#pluto22.com
3 pluto pluto#pluto.com
4 caio pippo#pippo.com
1 pippo pippo#pippo.com
2 pippo pippo2#pippo2.com
Result 6 and 3 share the same customer_name
Result 4 and 1 share the same customer_email_address
Result 1 and 2 share the same customer_name
Order_id 5 is not in results because it has no duplicates.

Try this:
SELECT
order_id,
customer_name,
customer_email_address
FROM
my_table
WHERE
order_id IN (
SELECT
order_id
FROM
my_table
GROUP BY
customer_name
HAVING
COUNT(*)>1
UNION
SELECT
order_id,
FROM
my_table
GROUP BY
customer_email_address
HAVING
COUNT(*)>1
)
ORDER BY
customer_name,
customer_email_address,
order_id DESC

Related

MySQL - remove non-duplicate rows from query

I'm running a query that selects details of orders, and I want to see only the orders that have gone through multiple stages. My data looks like:
id | order_id | action
1 100 1
2 100 2
3 100 4
4 101 1
5 102 2
6 103 1
7 103 2
So that only the rows for order_id 100 and 103 will be selected. This needs to be nested in a larger query.
You can use a subquery to get the orders that had multiple stages:
SELECT order_id
FROM your_table
GROUP BY order_id
HAVING COUNT(*)>1
then you can join this result back to your table:
SELECT o.*
FROM yourtable AS o INNER JOIN (
SELECT order_id
FROM your_table
GROUP BY order_id
HAVING COUNT(*)>1
) dup ON o.order_id = dup.order_id
Use group by with count and having
select *,count(order_id) as total from table
group by order_id
having total > 1
you can try this query:
select * from your_table
where ( select count(*) from your_table internal_table
where your_table .order_id = internal_table.order_id
) > 1

Select a unique combination of two columns

Sample data:
ProductID PackingID
------- ---------
1 2
1 2
3 2
3 2
1 1
2 1
3 2
I have the above sample data. What i want is to select the unique (not distinct) rows of the combination productID and packingID. In the above example the only matching results are
ProductID PackingID
------- ---------
1 1
2 1
These rows are the only unique combinations of ProductID and PackingID together. I do not want Distinct results because it will give me one of all the other combinations.
SELECT PRODUCTID,PACKINGID FROM DTEMP
GROUP BY PRODUCTID,PACKINGID
HAVING COUNT(PRODUCTID)=1
ORDER BY 1;
You can try this one this is how i do in oracle ... to get the unique rows without using distinct.
SELECT ProductID, PackingID
FROM yourtable
GROUP BY ProductID, PackingID
HAVING COUNT(*) = 1
your table should be like:
uniqueID ProductID PackingID
1 x y
2 x y
3 z x
Query:
SELECT uniqueID,ProductID,PackingID
FROM yourtable
WHERE uniqueID IN
(
SELECT MIN(uniqueID)
FROM yourtable
GROUP BY ProductID,PackingID
)

List by project_id using queries

I have a a table with following values:
id employee_id project_id
1 1 1
2 2 1
3 3 1
4 4 1
How can i list by project_id? using mysql queries?
project_id employee_id employee_id employee_id employee_id
1 1 2 3 4
Try below query-
SELECT project_id ,SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',1) AS employee_id1,SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',2),',',-1) AS employee_id2, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',3),',',-1) AS employee_id3, SUBSTRING_INDEX(SUBSTRING_INDEX(GROUP_CONCAT(employee_id),',',4),',',-1) AS employee_id4 FROM my_table GROUP BY project_id;
SELECT project_id, GROUP_CONCAT(employee_id) FROM Table GROUP BY project_id;
It's can be consider ..??

MySQL: Select Order Only if all its Items are ready status

I will simplify the table like this:
table order:
order_id | name
1 a
2 b
3 c
Table order_item:
item_id | fk_order_id | status
1 1 0
2 1 1
3 2 1
4 2 1
5 3 0
Ready status let say is 1, so only order_id=2 has all its items are on ready status.
How can I query select it?
There are a couple ways of doing this -- here is one using COUNT -- gets COUNT of all and compares to COUNT of Status = 1:
SELECT fk_order_id
FROM (
SELECT fk_order_id,
COUNT(1) totCount,
COUNT(CASE WHEN Status = 1 THEN 1 END) statusCount
FROM Order_Item
GROUP BY fk_order_id
) t
WHERE totCount = statusCount
SQL Fiddle Demo
This could be consolidated into a single query, but I think it reads better using a subquery.
Try this:
SELECT order_id ,name FROM order,order_item
WHERE order.order_id =order_item.fk_order_id
group by order_id ,name having min(status)=1

Joining multiple Rows and summing them With MySQL (and PHP)

I have a table structure similar to this:
id | order1_id | order1_type | order1_amount | order2_id | order2_type | order2_amount
------------------------------------------------------------------------------------------
1 1 3 4 1 4 5
2 2 1 1 1 3 2
3 1 4 4 2 2 1
I want to get the data like this:
order_id | order_type | order_amount
1 3 6
1 4 9
2 1 1
2 2 1
I want to group by type, and sum the order amounts. How can I do that ?
Thanks,
I'm going to use a union in a subquery to line the columns up, then group and sum on that.
Assuming you're stuck with this less-than-ideal table structure, you may want to create a view that represents the subquery below, then run the group by/sum against that view. I'm guessing such a view might be useful in more places than just this one query.
select t.order_id, t.order_type, sum(t.order_amount)
from (select order1_id as order_id, order1_type as order_type, order1_amount as order_amount
from orders
union all
select order2_id as order_id, order2_type as order_type, order2_amount as order_amount
from orders
union all
select order3_id as order_id, order3_type as order_type, order3_amount as order_amount
from orders
union all
select order4_id as order_id, order4_type as order_type, order4_amount as order_amount
from orders
union all
select order5_id as order_id, order5_type as order_type, order5_amount as order_amount
from orders) t
group by t.order_id, t.order_type
The easy way would be to use a view to untangle the table of questionable design, then group and sum on the view.
create view normalized_orders as
select order1_id as order_id,
order1_type as order_type,
order1_amount as order_amount
from your_table
union all
select order2_id as order_id,
order2_type as order_type,
order2_amount as order_amount
from your_table
Then you can do this:
select order_id, order_type, sum(order_amount)
from normalized_orders
group by order_id, order_type
order by order_id, order_type