Home > Net >  SQL inner join performance data duplicate
SQL inner join performance data duplicate

Time:04-30

table -- > qt

qstnId tagId
1 1
1 2
2 3
2 4
2 2

table --> question

qid
1
2

qid is primary key and qstnId is Fk

now when i'm running query -->

mysql> select tagId from qt inner join question on qt.qstnId = 1;

it's returning;

tagId
2
1
2
1

My question is why am i getting duplicate data here.

CodePudding user response:

Filtering

qt.qstnId = 1 is a filtering clause; it belongs in a WHERE clause.

Relation

qt.qstnId = question.qid states how the tables are related via a JOIN. It belongs in an ON clause after the JOIN.

Fix your query; if you still have concerns; provide SHOW CREATE TABLE so we can see if you have the necessary indexes (for performance).

CodePudding user response:

SELECT tagId 
FROM qt 
INNER JOIN question on qt.qstnId = question.qid
WHERE qid=1;
  • Related