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.
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 last month.
Improve this question
The common parameter in all tables is ID (primary key).
Now I would like that the ID also appears in the export and at best then arrange all entries ascending with the ID.
But this seems to be not possible. PhpMyAdmin gives the error message
#1052 - Field 'ID' in field list is not unique.
Is there a way to export the ID as well?
My function has the following structure:
SELECT name, age, date, ID FROM table t1 INNER JOIN table t2 ON t1.ID = t2.ID INNER JOIN table t3 ON t2.ID = t3.ID ORDER BY t1.ID ASC;
Table 1
ID
name
Table 2
ID
age
Table 3
ID
date
Your SELECT ID part seems vague. Perhaps be explicit and state t1.ID.
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
[![table named teams and Matches ][2]][2]I have 2 tables
[2]: https://i.stack.imgur.com/7v4nT.png**strong text**
I need data from these 2 tables in such a ways that a new table is formed with the following data
mid, result, date, team1id, team2id from matches table on the basis of tourid=6 and from table of team i need to show the Tname as team1 and Tname as team2.
Sounds like you need a join:
select matches.*,t1.Tname as team1,t2.Tname as team2 from matches
join team t1 on t1.Teamid=matches.team1id
join team t2 on t2.Teamid=matches.team2id
where tourid=6
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 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 the following table schemas:-
table1:
id1(primary key)
col1
table2:
id2(pk)
col2
table3:
id3(pk)
col3
junctable:
id1(fk),id2(fk),id3(fk),juncfield
Primary keys of table1,table2 and table3 are stored in another table called junctable. Now, I want to create a view that has the columns: col1,col2 and col3 from respective tables and juncfield from junctable.
Can someone tel me how to perform a join operation with the junction table and create a view?
Try this way, using LEFT JOIN:
SELECT T1.Col1,T2.Col2,T3.Col3,J.Juncfield
FROM junctable J LEFT JOIN
Table1 T1 ON J.id1=T1.id1 LEFT JOIN
Table2 T2 ON J.id2=T2.id2 LEFT JOIN
Table3 T3 ON J.id3=T3.id3
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