Take the 2-minute tour ×

I am writing an SQL query in order to fetch data, which is to be populated in the scrollable table .

Content id's of the contents which are going to be compared(Content1, Content2, Content3 .. in the image above) will be the input to the query.

Since, for comparison minimum 2 items are required, the number of id's passed to the query will always be 2 or greater than 2.

Following is the SQL of 3 tables, from which the required data is to be fetched.

Following table contains the Paramter names that are to be compared:

CREATE TABLE tbl_content_commons (
id integer PRIMARY KEY,
content_common_code char(20) NOT NULL,
content_common_name char(100) NOT NULL // The comparison label

Following table contains the code of comparison label(content_common_name char) in the table above and the Content id of the contents(which would be passed as the parameter for the query)

CREATE TABLE tbl_comparison_values (
id integer PRIMARY KEY,
tbl_content_common_id integer NOT NULL,// ID's of the Contents under comparison
userneed_comparison_label_id  integer NOT NULL,// ID of comparison label in the table above
value char(50) NOT NULL// Value corresponding to a comparison label - if it exists for a given content id 

Finally, the table containing the name of the contents(Content1, Content2..) whose id's were passed as parameters to the query

CREATE TABLE userneed_comparison_labels (
id integer PRIMARY KEY,
name  char(50) NOT NULL// Name of the content whose id's are passed through queries. content ID  in the table above

I have made enough of efforts in writing a query to fetch data that would help me populate the table shown in the attached image, but did not succeed. I can show the queries I have written, but since it again prolongs the question, I am not posting here.

Any guidance or help as to how to proceed, writing this SQL query would be much appreciated.

share|improve this question
Your description is incomprehensible. Show some example records and the desired result. –  CL. May 17 '13 at 20:21
I doubt people will download your SQLite database and try it themselves, because of security concerns. I would recommend setting up a SQL Fiddle with a schema and some initial data - that's much safer, and forkable too. –  halfer May 17 '13 at 20:39
It's not clear how the three tables relate to each other. Please describe foreign keys or other join expressions. –  PM 77-1 May 17 '13 at 20:48
add comment

1 Answer

up vote 1 down vote accepted

How about this...

select c.content_common_name,
from userneed_comparison_labels l
left join tbl_comparison_values v on l.id = v.userneed_comparison_label_id
left join tbl_content_commons c on c.id = v.tbl_content_common_id
where c.id in (1, 2, 3)

See SQL Fiddle for more detail.

Choose SQLLite (SQL.js) version. Click cancel if it asks if you want to use WebSQL.

share|improve this answer
bang on! thanks. –  user1400538 May 18 '13 at 13:38
add comment

Your Answer


