Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two tables
Table A : Content Question , its options and correct answer
Table B : This table is for time allow to answer the question in given time.
this table has Question_Id field which either have question id or zero. zero means if for Table A Question Id is not found in Table B then default Time will be Table B's Question_Id=0 > 5 Min
Now I want the data like Result table from query. By using Select Query with Join I am getting question details, which are matched with question Id (1,2,4) means for Question 3,5,6 row not getting that showing in result table.
Please suggest what sql query should write so that I can get result like Result Table's content.
I change your tables to small and simple tables and you can see the result in:
SQL Fiddle
or try this query:
SELECT t1.questionid,
t1.question,
t1.options,
t1.answer,
COALESCE(t2.timingstatement, '5 Min') TimingStatement
FROM tablea t1
LEFT OUTER JOIN tableb t2
ON t1.questionid = t2.questionid;
Try this
SELECT Q.QuestionID,Q.Question,Q.Options,QAnswer,
CASE WHEN Q.QuestionID NOT IN (SELECT QuestionID FROM Table2) THEN '5 Min'
ELSE T.TimingStatement
END [TimingStatement]
FROM Table1 Q
JOIN Table2 T ON Q.QuestionID = T.QuestionID
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a problem about writing a nested Select queries in sql.
I tried to devise a new table to show their counts of the tables but I couldn't complete the rest part. I show it as "...."
How can I do that?
Here is the code snippet shown below.
SELECT
(SELECT COUNT(DISTINCT u.id) AS totalUsers FROM Users u),
(SELECT COUNT(DISTINCT c.id) AS totalCategories FROM Categories c)
....
I want to get this result
New Table
totalUsers totalCategories ...
60 10 ...
First you need to create a table (but check first if it exists, I leave it up to you)
CREATE TABLE CountTable (
TotalUsers int,
TotalCategories int
);
Then insert the result of your query to the new table:
INSERT INTO CountTable (TotalUsers, TotalCategories)
SELECT
(SELECT COUNT(DISTINCT u.id) AS totalUsers FROM Users u),
(SELECT COUNT(DISTINCT c.id) AS totalCategories FROM Categories c)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
MySQL. I'm working with database and i have 2 tables. 1st table is mane, it has id and NAME. 2nd connected with 1st via FOREIGN KEY and has COST. How to get such request that has NAME of 1st and sum of COST of 2nd tables?
Just join, group by and sum(). Assuming that the foreign key column in table2 is t1_id:
select t1.name, sum(t2.cost) total_cost
from table1 t1
inner join table2 t2 on t2.t1_id = t1.id
group by t1.id, t1.name
If you want to allow names that have no match in table2, then use left join instead, and maybe coalesce(sum(t2.cost), 0) as well.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Trying to understand SQL more as part of a POC I'm working on and have hit a snag. I have two select queries (shown below) and I want to combine these into a single query that:
Shows all of the results of query one
Joins the result of query two if the category column matches.
Provides a default of 0 if there is no match
Query one:
SELECT activityId, location, category, activityScore FROM activities WHERE location = "manchester";
Query two:
SELECT userId, category, userScore FROM userscore s WHERE userId = "32976428";
Expected Output:
The resulting query should show all activities in "manchester" along with the associated userScore if the specified use has one that matches that category. If there is no userscore then 0 should be returned instead.
Thanks for any help.
Carl
I think you need a LEFT JOIN on your userscore table
SELECT a.activityId, a.location, a.category, a.activityScore,
s.userId, ISNULL(s.userScore,0) as userScore
FROM activities a
LEFT JOIN userscore s ON s.category = a.category AND s.userId = "32976428"
WHERE a.location = "manchester";
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My query:
SELECT DISTINCT img.prd_id,prd.*,img.* FROM products_prd prd OUTER JOIN
prd_images_img img ON prd.prd_id=img.prd_id ORDER BY prd.prd_datetime
the primary key in products_prd is returned NULL. They have values. while img.prd_id may be NULL or not.
The problem is that you have name collisions -- two things called prd_id for example. You need to use aliases to rename the columns:
SELECT prd.*, img.col1 as img_col1, img.col2 as img_col2
FROM products_prd prd LEFT OUTER JOIN
prd_images_img img
ON prd.prd_id = img.prd_id
ORDER BY prd.prd_datetime;
You don't need to select prd_id twice.
Or, you can use USING instead of ON:
SELECT *
FROM products_prd prd LEFT OUTER JOIN
prd_images_img img
USING (prd_id)
ORDER BY prd.prd_datetime;
This returns the columns in the USING clause only once (although you could have problems with other columns if they have the same names in the two tables).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is my current code
SELECT * FROM pn_queue WHERE email = '2'
Now i want to extend is with an "AND" wich is in another table. Like this:
SELECT * FROM pn_queue WHERE email = '2' AND FROM pn_cats WHERE cat = '2'
How can i make it? I will be thankfull for any ideas or solutions.
You need to use a Join
select * from pn_queue a
join pn_cats b on a.pn_cats_id = b.id -- Change this condition to whatever should match on both table.
where b.cat = '2'
and a.email = '2'
Not sure if the join condition is actually right as you don't give enought information, but just modify the condition a.pn_cast_id = b.id if it ain't.
Edit : Just realized in your example cat and email are both equals to 2 so if it should be the match value then the correct query would simply be :
select * from pn_queue a
join pn_cats b on b.cat = a.email
For more information on SQL Join, you should check this thread What is the difference between "INNER JOIN" and "OUTER JOIN"?. It explain the difference between all possible joins with diagrams/examples etc.