I have a table structure similar to this in an SQLITE database, with the LANID being a FK to another table:
ID LANID TEXT
-------------------------
1 101 Issue1
1 102 Comment1 for Issue1
2 101 Issue2
2 102 Comment1 for Issue2
3 101 Issue3
3 102 Comment1 for Issue3
1 102 Comment2 for Issue1
...
I am trying to fetch values from the TEXT field that share the same ID, using queries like this:
SELECT t.TEXT as issue, s.TEXT as comment
FROM MyTable as t, MyTable as s
WHERE t.LANID = '101' and s.LANID = '102' AND t.ID = s.ID
AND t.TEXT like "%some text in Issue1%"
and it almost works, but with one problem: it can't "duplicate" values when needed. In other words, I can't display the second comment for the same issue like this:
issue comment
---------------------------------
Issue1 Comment1 for Issue1
Issue1 Comment2 for Issue1
...
Thanks in advance for any hints.
I would use an explicit self join
, instead of the implicit join
used in your original query:
SELECT
mt1.Text AS Issues,
mt2.Text AS Comment
FROM MyTable mt1
JOIN Mytable mt2
ON mt1.ID = mt2.ID
AND mt1.LANID = 101
AND mt2.LANID = 102
I think the problem is really about how you've set up your database. Comments should be in a seperate table referencing the issues with a foreign key. Without a sound database design, you'll always be struggling with kind of thing.
Your query does show the second comment. See this example at SQL Fiddle .
For a better way to write a join
, see Michael Fredrickson's answer .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.