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
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
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 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
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 9 years ago.
Improve this question
In mySQL Table A has 10 rows and table B has 10 rows. The product of Table A and Table B would have 100 rows?
I am stuck.
If table1 has N rows and table2 has M rows, and you do:
SELECT * FROM table1 A, table2 B
you get N X M rows.
Check if you like Generating a Cartesian Product.
What exactly is the product of two tables? If you join them loosely and select one column from each theb yes. But it makes no sense to do it.
You are probably not joining them properly. If you could post your code it would help.
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 9 years ago.
Improve this question
I have 2 tables catelog and catelog copy. And, I need to to display both these table fields as one table using mysql.
Can anyone correct this code for joining 2 tables?
$result = mysql_query("SELECT * FROM catelog WHERE title='".$fileid."'union
select status from catelog_copy " );
SELECT statements you'd like to UNION have to return the same number of columns and types of those columns should match.
So
SELECT a, b, c FROM table1 UNION SELECT a, b, c FROM table2
will work, but
SELECT a, b FROM table1 UNION SELECT a, b, c FROM table2
will not.
Names of the fields and the fields themselves might be different though.