Join related Issue - mysql

New to SQL
Suppose we have two tables
One has got the ID and Name column :
+----+-------+
| ID | Name |
+----+-------+
| 1 | Sam |
| 1 | Dan |
+----+-------+
and the second one has also got two columns as follow :
+----+------------+
| ID | Relatives |
+----+------------+
| 1 | Uncle |
| 2 | Aunty |
+----+------------+
If we do inner join we would only get the rows where the condition satisfies. But i want the output to be Like
+------+------------+
| ID | Relatives |
+------+------------+
| 1 | Uncle |
| NULL | Aunty |
+------+------------+
once only the value in the ID column should be shown. If the occurrence is twice or thrice it should come as null.
Just tell me if it is possible or not? and How for both the cases.

As your question is not clear, so assuming that you need to retrieve id from table a and name from table b and you also want to avoid duplicate rows, then an option could be to use distinct along with left join:
select distinct a.id, b.name
from b
left outer join a
on b.id = a.id
order by id desc
Result:
+------+-------+
| id | name |
+------+-------+
| 1 | Uncle |
| NULL | Aunty |
+------+-------+
DEMO

Try this:
SELECT
T1.Id,
T2.Relatives
FROM SecondTable T2
LEFT JOIN FirstTable T1
ON T1.ID = T2.ID
GROUP BY T1.Id,
T2.Relatives
This is what I get exactly:
CREATE TABLE #a (
id int,
name varchar(10)
)
CREATE TABLE #b (
id int,
name varchar(10)
)
INSERT INTO #a
VALUES (1, 'sam')
INSERT INTO #a
VALUES (1, 'Dan')
INSERT INTO #b
VALUES (1, 'Uncle')
INSERT INTO #b
VALUES (2, 'Aunty')
SELECT
T1.Id,
T2.name
FROM #b T2
LEFT JOIN #a T1
ON T1.ID = T2.ID
GROUP BY T1.Id,
T2.name
DROP TABLE #a
DROP TABLE #b
Output:
Id name
NULL Aunty
1 Uncle
Hope, this is what you ask in your question.

Related

SQL filter entries which match all entries from another table

I am using MySQL. Let's say I have these two tables:
table 1
+---------+
| product |
+---------+
| 1 |
| 2 |
+---------+
table2
+------+---------+
| name | product |
+------+---------+
| A | 1 |
| A | 2 |
| B | 1 |
| B | 3 |
| C | 1 |
+------+---------+
which are produced using the following code:
CREATE TABLE table1(
product INT
);
CREATE TABLE table2(
name VARCHAR(10),
product INT
);
INSERT INTO table1 VALUES(1);
INSERT INTO table1 VALUES(2);
INSERT INTO table2 VALUES('A', 1);
INSERT INTO table2 VALUES('A', 2);
INSERT INTO table2 VALUES('B', 1);
INSERT INTO table2 VALUES('B', 3);
INSERT INTO table2 VALUES('C', 1);
I would like to produce a table with names from table2, for which its products match all products of table1. In this case, simply
+------+
| name |
+------+
| A |
+------+
That's the name of the retailer for which all products match the ones in the other table.
This is probably something simple that I am failing to see. I have tried inner joins, using all with a subquery, ... but...
I ended up being able to solve this using:
SELECT nome
FROM table2
WHERE product IN (SELECT product FROM table1)
GROUP BY nome HAVING COUNT(*) = (SELECT COUNT(*) FROM table1);
Based on check if a column contains ALL the values of another column - Mysql
create table3 then execute the following
INSERT INTO table3 (name)
SELECT DISTINCT t2.name
FROM table2 t2
LEFT JOIN table1 t1 on t2.product = t1.product
WHERE t1.product IS NOT NULL
You can use a join to get any matches. And then having to check they are all there.
Assuming no duplicates:
select t2.name
from table2 t2 join
table1 t1
using (product)
group by t2.name
having count(*) = (select count(*) from table1);

Create a Join leaving one row for any possible duplicate

I have a 2 tables that looks like this
How can I call the same column wihout duplicating it
TYSM for help
Please try below mentioned Query:
select distinct t2.data,t1.key,t1.data from t1.table1 JOIN table as t2 ON t1.key = t2.key
You could assign a row number using a variable to t2 then join to t1 supressing the output of the t1.key.
for example
drop table if exists t1,t2;
create table t1 (id int);
create table t2 (id int, name varchar(2));
insert into t1 values(1),(2),(3),(4);
insert into t2 values(1,'s1'),(1,'s2'),(2,'s3'),(3,'s4'),(4,'s5');
select s.id, s.name,
case when s.rn = 1 then s.rn
else ''
end as something
from t1
join
(
select t2.id,t2.name,
if(t2.id <> #p, #rn:=1,#rn:=#rn+1) rn,
#p:=t2.id
from t2,(select #rn:=0,#p:=0) r
) s on t1.id = s.id
order by t1.id, s.name
Result
+------+------+-----------+
| id | name | something |
+------+------+-----------+
| 1 | s1 | 1 |
| 1 | s2 | |
| 2 | s3 | 1 |
| 3 | s4 | 1 |
| 4 | s5 | 1 |
+------+------+-----------+
5 rows in set (0.00 sec)

Odd behavior of max and having in MySQL when max==0

I have the following table:
mysql> select * from foo;
| id | value | bar |
+----+-------+------+
| 1 | 2 | 3 |
| 2 | 0 | 3 |
| 1 | 1 | 5 |
I want to select the tuple with the maximum value for each id. However, when max(value) is 0, I don't get a result.
mysql> select id,max(value),bar from foo group by id having max(value);
| id | max(value) | bar |
+----+------------+------+
| 1 | 2 | 3 |
Is this supposed to behave like that and if so, why?
HAVING cannot be used in any way to pick a record out of a group of records as defined by the fields used in the GROUP BY clause. It is rather applied to the group as a whole.
So, in your case, you have to do a self-join to get the rest of the table fields:
select t1.id, t1.value, t1...
from foo as t1
join (
select id, max(value) as max_value
from foo
group by id
) as t2 on t1.id = t2.id and t1.value = t2.max_value
IMHO you can get MAX couple by multiplying (id x value).
create table foo(id int, value int);
insert into foo values
(2,0),
(1,0),
(2,1),
(3,0),
(2,2);
select id, value
from foo
order by (id * value) desc
limit 1;
id | value
2 | 2
drop table foo;

Preserve order of SQL WHERE IN() clause with nested SELECT

I need a list of item names ordered by count of items. Item names and corresponing id's are stored in tabletwo while tableone refers to items by id's:
tableone tabletwo
+--------+-----------+ +----+------+
| itemid | condition | | id | name |
+--------+-----------+ +----+------+
| 2 | satisfied | | 1 | foo |
+--------+-----------+ +----+------+
| 1 | satisfied | | 2 | bar |
+--------+-----------+ +----+------+
| 3 | satisfied | | 3 | hurr |
+--------+-----------+ +----+------+
| 3 | satisfied | | 4 | durr |
+--------+-----------+ +----+------+
| 3 | satisfied |
+--------+-----------+
| 4 | satisfied |
+--------+-----------+
| 4 | satisfied |
+--------+-----------+
| 3 | nope |
+--------+-----------+
| 1 | satisfied |
+--------+-----------+
SQL code:
SELECT `itemname` FROM `tabletwo` WHERE `id` IN (
SELECT `itemid` FROM (
SELECT count(`itemid`), `itemid`
FROM `tableone`
WHERE `some_codition`="satisfied"
GROUP BY `itemid`
ORDER BY count(`itemid`) DESC
) alias
)
The nested SELECT returns a list of item id's in descendant order: 3, 4, 1, 2. This list is then used as an argument of an IN() clause. The expected result of the whole query is: hurr, durr, foo, bar (in this exact order). But the order is not preserved. I know it can be done like this: ORDER BY FIELD(id, 3, 4, 1, 2) but I don't know how to do this trick when the ordered list is fetched dynamically like in my case. Do I need to SELECT it again? Or temporary table maybe? Or is it better to build another query outside SQL?
Try using JOIN instead:
SELECT t2.`itemname`
FROM `tabletwo` AS t2
JOIN (
SELECT count(`itemid`) AS cnt, `itemid`
FROM `tableone`
WHERE `some_codition`="satisfied"
GROUP BY `itemid`
) AS t1 ON t1.`itemid` = t2.`id`
ORDER BY t1.cnt DESC
You can create a derived table using the subquery of the IN operator and perform a JOIN to this table, so that you are able to use the COUNT in the ORDER BY clause of the main query.
Use JOIN instead of IN:
SELECT
t2.name
FROM tabletwo t2
LEFT JOIN tableone t1
ON t1.itemid = t2.id
AND t1.`condition` = 'satisfied'
GROUP BY
t2.id, t2.name
ORDER BY COUNT(*) DESC
If you want to exclude rows from tabletwo that do not have a match on tableone, use INNER JOIN instead of LEFT JOIN.
ONLINE DEMO

copy from t1 into new table t2 filtering out duplicate rows

I have a table t1, some rows have duplicates in all columns EXCEPT id.
t1's id is AUTO_INCREMENT and has 1MIL rows.
t2 is a new table without data and the id does not need AUTO_INCREMENT as i will probably create a new column for this.
Q: After i create t2, how can i copy from t1 into t2 where distinct values from t1 in all columns, so that t2 has no duplicate rows
I am on amazons RDS ENGINE=InnoDB
t1 - this is what i have
+---+-----+-----+------+-------+
|id |fname|lname|mytext|morevar|
|---|-----|-----|------|-------|
| 1 | joe | min | abc | 123 |
| 2 | joe | min | abc | 123 |
| 3 | mar | kam | def | 789 |
| 4 | kel | smi | ghi | 456 |
+------------------------------+
t2 - this is what i would like to end up with
+---+-----+-----+------+-------+
|id |fname|lname|mytext|morevar|
|---|-----|-----|------|-------|
| 1 | joe | min | abc | 123 |
| 3 | mar | kam | def | 789 |
| 4 | kel | smi | ghi | 456 |
+------------------------------+
this is my attempt, but got: Error Code: 1136. Column count doesn't match value count at row 1
INSERT INTO t2 (id,fname,lname,mytext,morevar)
SELECT DISTINCT st.mytext
FROM t1 st
WHERE st.id>0
AND st.id<=1000
The easiest way is to use group by:
INSERT INTO t2 (id,fname,lname,mytext,morevar)
SELECT id,fname,lname,mytext,morevar
FROM t1 st
WHERE st.id>0 AND st.id<=1000
group by mytext;
However, technically, this is not correct because the column values are not guaranteed to come from the same row. So, the right way is:
INSERT INTO t2 (id,fname,lname,mytext,morevar)
SELECT st.id, st.fname, st.lname, st.mytext, st.morevar
FROM t1 st join
(select mytext, min(id) as minid
from t1
group by mytext
) mint
on st.id = minid
WHERE st.id>0 AND st.id<=1000 ;
Use GROUP BY to make just that column distinct.
INSERT INTO t2 (id,fname,lname,mytext,morevar)
SELECT id, fname, lname, mytext, morevar
FROM t1 st
WHERE st.id>0
AND st.id<=1000
GROUP BY mytext
If the other columns differ between the duplicates, it will pick values from them arbitrarily (not necessarily from the same rows, even).
Try this code, it will select the smallest id number of duplicates and takes into account all columns.
INSERT INTO t2 (id,fname,lname,mytext,morevar)
SELECT min(id) id, fname, lname, mytext, morevar
FROM t1
WHERE t1.id > 0 and t1.id <= 1000
GROUP BY fname, lname, mytext, morevar
ORDER BY id;