Fetch duplicate rows with SQL - mysql

I am trying to develope a query to fetch the rows having duplicate values, i need to fetch both records i.e duplicating record and the real one, for example
table
id keyword
-------------------
1 Apple
2 Orange
3 Apple
4 Grape
5 Banana
6 Grape
The query result should be:
id keyword
-------------------
1 Apple
3 Apple
4 Grape
6 Grape
Please anyone help me!

Query:
select * from
table where keyword in
(select keyword
from table
group by keyword
having count(keyword)>1)

One way to do it:
SELECT *
FROM `table` t1
WHERE
(SELECT COUNT(*) FROM `table` t2 WHERE t2.keyword = t1.keyword) > 1
And another way:
SELECT t1.*
FROM `table` t1
JOIN `table` t2 ON t1.keyword = t2.keyword
WHERE t1.id != t2.id

This might help:
SELECT t1.id, t1.keyword
FROM table t1
INNER JOIN table t2
ON t1.id != t2.id
AND t1.keyword=t2.keyword
Tested on SQL Fiddle
http://sqlfiddle.com/#!2/44dbb/1/0

Related

Left join with other table with where clause

I have 2 tables in MySQL database that I would like to join, where I would like to contain all results from table1.
My tables looks like this:
table1
id
name
1
name1
2
name2
3
name3
4
name4
5
name5
6
name6
7
name7
8
name8
table2
id
table1_id
myfield
1
3
test1
2
2
test2
3
1
test1
4
4
test2
5
5
null
6
2
null
What I am trying to achieve is to get a table which contains all the rows from table1 and only the data that is joined from table2.
This is my query:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
where myfield="test1"
group by t1.id
But this is not what I want to achieve.
What I want to achieve is to get all records from table1 and to have all related records from table2 where table2.myfield="test1". And for the other for table2.mytable to have null (if they do not fulfil table2.myfield="test1").
Any help is much appreciated!
Thanks!
move the where clause to the on clause:
select * from table1 as t1
left join table2 as t2 on t1.id = t2.table1_id
and myfield="test1"
group by t1.id
BTW: some DBMS does not allow select * with group by. So select the id and some aggregated values or remove group by id

Correlated subquery & conditional where clause in Sequelize

I have a correlated subquery that is validated based on the column value. Even though I read through the sequelize docs and examples on GitHub, I did not really get any working solutions.
Query I am trying to execute
select count(*) from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
Any references or suggestions are appreciated.
Update:
The goal is to fetch all records from table1, but if deciding_col value is true for a record, then I need to check if that particular record exist in table2.
Example:
Table1 Data:
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
Table2 Data:
id | second_condition
1 132
Output:
1.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=132)
else 1=1 end;
id | name | deciding_col
1 abcd 1
2 edfg 0
3 xyz 0
2.
select t1.* from table1 t1
where case when deciding_col=1 then t1.id in
(select distinct t2.id from table2 t2 where t2.id=t1.id and t2.second_condition=133)
else 1=1 end;
id | name | deciding_col
2 edfg 0
3 xyz 0

Can I select rows from two tables using t1 rows as key for t2 rows?

I have two tables:
id1, id2, ... idX contains id from t2 or null. Can I make such select, which will put params from t2 in t1?
For example i have a t1(name,year,id1,id2,id3,id4) values:
name year id1 id2 id3 id4
------------------------------------------
marko 1999 1 2 null 1
polo 1985 null null 5 3
And t2 values:
id info param
--------------------------------------
1 apple green
2 car yellow
3 bee pink
4 doctor whiskey
5 book small
So I'd like to have such dynamical query results, based on t1 rows:
SELECT name, year, id1-info, id1-param, id2-info, id2-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
SELECT name, year, id3-info, id3-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
I googled a lot, but found nothing except nested queries, such as:
SELECT
name,
year,
(SELECT `info` FROM t2 WHERE id = t1.id1) AS id1-info,
(SELECT `param` FROM t2 WHERE id = t1.id1) AS id1-param,
(SELECT `info` FROM t2 WHERE id = t1.id2) AS id2-info...
But I understand, that it is a very bad idea, because I have a lot of id columns in t1, which are not static. Or if it cannot be done dynamically, can I just make SELECT, which will show me all in one row:
SELECT name, year, id1-info, id1-param, id2-info, id2-param, id3-info, id3-param, id4-info, id4-param
FROM t1 WHERE name = 'marko'
To use one select in one row, you can use mulitple left join on t2.
SELECT name,
`year`,
id11.info as `id1-info`,
id11.param as `id1-param`,
id21.info as `id2-info`,
id21.param as `id2-param`,
id31.info as `id3-info`,
id31.param as `id3-param`,
id41.info as `id4-info`,
id41.param as `id4-param`
FROM t1
LEFT JOIN t2 as id11 on t1.id1=id11.id
LEFT JOIN t2 as id21 on t1.id2=id21.id
LEFT JOIN t2 as id31 on t1.id3=id31.id
LEFT JOIN t2 as id41 on t1.id4=id41.id
WHERE name = 'marko';
result:
name year id1-info id1-param id2-info id2-param id3-info id3-param id4-info id4-param
marko 1999 apple green car yellow (null) (null) apple green

selecting one column from 3 different tables

I have 3 tables:
Table 1: columns are:
+------+-------+----------+----------+
| date | time | user_id | channel |
+------+-------+----------+----------+
Table 2:
+---------+------------+
| user_id | id _number |
+---------+------------+
Table 3:
+---------+-------+
| channel | sort |
+---------+-------+
In table 1 the columns are sorted as following :
2011-07-29, 12:35:15.650, 22, DeluxeMusic
In table 2 :
130.83.10.c42ce82365b9f6d , 22 (same as user_id in table 1)
In table 3:
DeluxeMusic (same as in table 1), entertainment.
The columns user_id in table 2 : 130.83.10.c42ce82365b9f6d means a user_id of some user. From this user ID I need to get all entries with the value of 130 in the begining. For all entries with same value 130.
Then I need to seek for this value of 130 in the table 3 the sort of channel they are watching, and to count by that all users watching a channel typ.
Channel typs are also : sport, shopping, entertainment and so on.
you could try this too:
select count(*) from t1 join t2 on t1.id = t2.id and t2.userid like '130%' join
t3 on t1.channel = t3.channel group by t1.channel
sql fiddle here
Edit:
another version:
select count(t1.id) , t1.*, t2.*, t3.*
from t1 join t2 on t1.id = t2.id join
t3 on t1.channel = t3.channel and t2.userid
like '130%' group by t3.channel, t1.id
fiddle
Not sure if I get your question completely. But from what I understand you would be needing something like below:
SELECT COUNT (*) FROM TABLE1 T1,TABLE2 T2,TABLE3 T3
WHERE T1.USER_ID = T2.USER_ID
AND T1.USER_ID LIKE '130%'
AND T1.CHANNEL = T3.CHANNEL
The above sql would be the used to fetch the count of the channels that the users with their use ids beginning with '130' would be watching.
Hopefully this should help you in someway.

Using join in place of this two seperate queries

I have a table by the following structure and records, All i want to do is to just extract list_name of those records whose uid is either 0 or 2 , but i also want to check if a record is available for uid 0 and 2 , then only it should show only record with uid 2... I have managed to do this with two queries... Can i write a single query for this...
**id** **uid** **list_name**
1 2 favourite list
2 0 Things i love
3 0 my list
4 2 my lists
5 3 test334
6 2 Things i love
Any help or suggestion will be highly appreciated ..Thanks in advance..
Another guess:
SELECT
uid, list_name
FROM
myTable T1
WHERE
uid = 2
OR
( uid = 0
AND NOT EXISTS
( SELECT *
FROM myTable T2
WHERE T2.uid = 2
AND T2.list_name = T1.list_name
)
)
A guess...
SELECT
list_name
FROM
myTable T1
WHERE
uid IN (0, 2)
AND
NOT EXISTS (SELECT * FROM myTable T2 WHERE T2.uid = 0)
UNION ALL
SELECT
list_name
FROM
myTable T1
WHERE
uid = 2
AND
EXISTS (SELECT * FROM myTable T2 WHERE T2.uid = 0)