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.
How about this...
select c.content_common_name,
l.name,
v.value
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.
Related
Edit2:
I like to get all values from 2 tables WHERE the value of highlight='1'
highligh is a column present in both tables. Both tables have an unique structure.
I want to get all data from both tables where highlight='1'
SELECT * FROM painting, instaview WHERE highlight='1'
...both give me the following error:
"Column 'highlight' in where clause is ambiguous"
I searched a lot and found a lot about this error but only complicated JOIN causes that are different than my case. i know the column is in both tables but I want the results of those 2 table queries joined.
Edit1:
As requested the structure of both tables:
Column Type Comment
id int(6) Auto Increment
category varchar(3)
filename varchar(30)
title varchar(200)
material varchar(200)
year varchar(4)
highlight tinyint(1)
active tinyint(1)
You need to say from which table, you are querying the highlight.
Can you try like this ?
SELECT * FROM painting JOIN instaview ON painting.highlight='1'
I did not know about the "qualify", but this joined my data in more columns, I just wanted the results of both tables. After more searching I found the keyword: UNION
SELECT * FROM painting WHERE highlight=1
UNION
SELECT * FROM instaview WHERE highlight=1
This did the trick!
(I like to excuse myself for not being very clear in my question, for me this was all very new)
If you want highlight='1' in both tables, and don't want to specify a table, you may use
SELECT *
FROM painting
JOIN instaview USING (highlight)
WHERE highlight='1'
The fields used in USING are "combined", and tablename qualification not needed.
PS. This is "bad practice" - specify tablenames for all fields when there are more than one table in whole query source (even when some fieldname is unique over data source - it is present in one table only).
I have created a table with several columns and a related form. In that form, I have created a combobox to show the columns of the table in differents rows in combobox.
I have 3 fields in my table: product1, product2 and product3 for a same order I have named with a number. When I create the combobox, values show in 3 differents columns in the same row so I can just select the data from the first row and column. But what I need is the field's data displayed in different rows instead showing data in different columns.
I researched in forums, and I read It can be solved with a join query and selecting that in row source in combobox menu, but I have tried it and I got the same result. I donĀ“t know what am I doing wrong. I'd appreciate your help.
Thanks in advance.
You have to pivot the results
this is an example of how to restructure using sub queries bad data designs
SELECT *
FROM
(
SELECT QualifierID -- <--- this is a unique identifier
,
(
SELECT QualifierText
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS QualifierText
,
(
SELECT [ExampleText]
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS ExampleText
,
(
SELECT [WhenModified]
FROM [QDB].[dbo].[Qualifier] x
-- this is how you tie the unique identifier and grab a specific element by itself
-- also pay attention to how we're making it equal to q.QualifierID,
-- which is the alias to the table below
WHERE QualifierID=q.QualifierID
) AS WhenModified
FROM [QDB].[dbo].[Qualifier] q -- this is a table alias
) z -- this is the alias fo the subquery combining everything together.
I have 2 tables, the first one of them is orderbook has a field namely
easy_order with datatype of tinyint(1) and the primary key is id.
The second table name is execution including the fields of buy_order_id and sell_order_id. These 2 fields are ref. from the id key from the orderbook table.
I would like to write a SQL to find all the rows from the execution table whose buy_order_id OR sell_order_id row from the orderbook table has the easy_order column value of 1.
I use MySQL database.
I would write it this way, avoiding repetition and dual nested selects:
select execution.*
from execution
inner join orderbook on
orderbook.id in (execution.buy_order_id, execution.sell_order_id) and
orderbook.easy_order = 1;
(You may prefer conceptually to put orderbook.easy_order = 1 in a where clause. It will produce the same results, so it's a matter of preference.)
I make it work The SQL is,
SELECT * FROM execution WHERE buy_order_id IN (SELECT id FROM orderbook WHERE easy_order=1)
OR
sell_order_id IN (SELECT id FROM orderbook WHERE easy_order=1);
I need to select * FROM sections and get the column values for every row to fill a JTable. My problem is that my adviserId column on section table is an INT
And because I'm getting the result set of every column on every row, I cannot issue a WHERE clause. I thought of subquery but since Id is different on every row, no predetermined Id can be supplied on WHERE clause.
So If I run my stored procedure, I get just an int value for adviserId instead of the teacher's name.
I have teachers and sections table.
Teacher
id PK INT
lastName
firstName
middleName
isAdviser
status
Sections
id PK
name
adviserId FK-- REFERENCING `id` column ON teacher table
What would be the best approach? I hope you can help.
Thanks.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I've created the final stored procedure based on everyone's suggestion. (THANKS AGAIN all.)
CREATE DEFINER=`root`#`localhost` PROCEDURE `getAllSectionsInfo`()
BEGIN
SELECT
s.`name` AS `Section Name`,
s.`session` AS `Session`,
CONCAT(t.lastName,',',t.firstName,' ',t.middleName) AS Adviser,
s.yearLevel AS `Year Level`,
CONCAT(syStart,'-',syEnd) AS SchoolYear
FROM sections s
INNER JOIN
teacher t on s.adviserId = t.id;
END
Yes I also think the same, that a simple inner join will do your job. Try the below example..
create table JTable as select T.id as Tid,T.lastName,T.firstName,T.middleName,T.isAdviser,T.status,S.id as Sid,S.name,S.adviserId
from Sections as S
inner join Teachers as T on T.id = S.adviserId
You can apply left join here to make sure that you have all records of Section table either related to Teachers data or with null data.
So, now the JTable will have all the columns in that you have put on the selection list.
Below is solution for db data selection
SELECT * FROM sections s INNER JOIN teacher on s.adviserId = t.id
I have a table that holds a list of tasks to be performed by a process. Each task only works on items that match the input status, and when the task is completed it changes the item's status to the output status.
To track these tasks I use a table like this.
CREATE TABLE `tasks` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(80) NOT NULL,
`job_type` varchar(45) NOT NULL,
`input_status` varchar(45) DEFAULT NULL,
`output_status` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
)
The task's statuses form a chain of events. Here is an example
NULL -> NEW
NEW -> CREATE
CREATE -> INSPECT
INSPECT -> VERIFY
VERIFY -> PUBLISH
In real life the list of tasks is very long. note: The types of different statuses are unknown to me, these user defined values.
When I view the table using an order of input_status the records show the tasks in the wrong order. Sorting by input_status and output_status also doesn't work (obviously).
How can I sort the table where null is first, followed by the chain of input_status to output_status?
I figure that I'll have to create a virtual field to hold an extra sorting value, but I'm not sure what it should be or calculated.
Here is what I've tried so far, but it doesn't work.
SELECT *,
(SELECT input_status FROM tasks AS parent
WHERE parent.output_status = tasks.input_status
) AS sorted
FROM tasks
ORDER BY sorted, input_status;
You can build a case statement on which you apply the sorting (not sure about the exact syntax however):
SELECT * FROM tasks
ORDER BY
CASE WHEN input_status IS NULL THEN '_'
ELSE output_status END ASC;
Of course you will have to adapt the case statement to your sorting needs.
I am not sure if you have defined a master relation on status values. If you had done it, query would have been easier.
Master Staus to hold status_id and status_text (null, new, verify, etc.).
Child tasks to to refer from master for input and output status id values.
Actually you need a reqursive query to do it. You can consider output_status as ID and input_status as a PARENT_ID. So you need to find full PARENT->CHILD path to assign an order number.
It can be done simple in MS SQL with recursive CTE but for MySQL as I know it isn't simple.
Try to use these UDF's to make sorting field for example [null->stat1->stat2->....]
Another approach if there is a fixed maximum status count you can do it using LEFT JOIN to connect previous records to find path to sort. Something like this (here are 3 levels of recursion):
SELECT tasks.*
FROM tasks
LEFT JOIN tasks t1 on t1.output_status=tasks.input_status
LEFT JOIN tasks t2 on t2.output_status=t1.input_status
LEFT JOIN tasks t3 on t3.output_status=t2.input_status
ORDER BY tasks.title,t3.output_status,t2.output_status,t1.output_status
Also here are links to consider:
Recursive self
query