Sql Query to Join Two Tables With No Repeated Values

MySQL - Joining two tables without duplicates?

It's much simpler than you may think:

select distinct(customer_id) from orders;

Edit: If you actually want to get the full info on the customer,

select * from customers where customer_id in (select distinct(customer_id) from orders);

SELECT with LEFT JOIN with two tables without duplicates

I am guessing that you want something like this:

select t2.*, t1.*
from t2 left join
(select t1.*, row_number() over (partition by email order by id desc) as seqnum
from t1
) t1
on t1.email = t2.email and t1.seqnum = 1;

row_number() is a window function that assigns a sequential number to rows with the same email -- with the highest id getting "1" and then incrementing as the id decreases. This identifies the row with the largest id and the seqnum = 1 retrieves only that row for each email.

Best way to combine two tables, remove duplicates, but keep all other non-duplicate values in SQL

SQL - select values from two tables but without duplicates

If you have several repeated rows you could try using DISTINCT

SELECT distinct uc.*,ul.NAME FROM UC AS uc
inner JOIN UL AS ul ON uc.PARTY_ID = ul.PARTY_ID
WHERE PID = '33322';

but based on your sample
if you want the result of all the names in a rows you could use group_concat

SELECT uc.*, group_concat(ul.NAME)
FROM UC AS uc
inner JOIN UL AS ul ON uc.PARTY_ID = ul.PARTY_ID
WHERE PID = '33322'
group by uc.id;

otherwise if the name value is not relevant you coul try using a fake aggregation function

SELECT uc.*, min(ul.NAME)
FROM UC AS uc
inner JOIN UL AS ul ON uc.PARTY_ID = ul.PARTY_ID
WHERE PID = '33322'
group by uc.id;

How can I SUM two tables with a join without duplicate data?

How to join two tables without duplicates

If you try just like this without any unique id's, then your full concept is wrong. Add some unique id's to your table and do the necessary coding.

If you have table structure like below your can write query with simple equi join.
Income_tbl:

date              income  id    
---------------------------
09/05/13 56000 1
09/05/13 66600 2
09/05/13 50000 3

Expense_tbl:

date              expense  id
----------------------------
09/05/13 68800 1
09/05/13 2
09/05/13 3

(or) try @Brian Hoover's query it will work.

 SELECT income.date_col, income.income, expense.expense
FROM (
SELECT i.date_col, i.income, @curRow := @curRow + 1 AS row_number
FROM Income_tbl i
JOIN (SELECT @curRow := 0) r
) AS income
JOIN (
SELECT e.date_col, e.expense, @curExpenseRow := @curExpenseRow + 1 AS row_number
FROM Expense_tbl e
JOIN (SELECT @curExpenseRow := 0) r
) AS expense
ON income.row_number = expense.row_number;

SQL Join Two Tables Remove Duplicates

You can use Inner join which will join tables such that it selects records that have matching values in both tables, write your query as

select distinct t1.mov_id, t1.mov_name, t2.actor_name from MOVIES t1 inner join ACTOR t2 on t1.actor_id=t2.actor_id_2;


Related Topics



Leave a reply



Submit