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 7 years ago.
Improve this question
How can i make this works?
I have 2 tables:
course:
course id,name
prereq:
course id,prereq id
What is the query for this result:
+----------+------------+----------+------------+
|Course ID |Name |Prereq ID |Prereq Name |
|1 |Intro into B| | |
|2 |Biology |1 |Intro into B|
|3 |Genetics |1 |Intro into B|
+----------+------------+----------+------------+
SELECT c.courseId, c.courseName, p.prereqId, pc.courseName
FROM course as c
// joins prerequisites
LEFT JOIN prereq as p on p.courseId=c.courseId
// joins course data to prerequisites
LEFT JOIN course as pc on p.prereqId=pc.courseId
You should be able to use a left join to pull these two tables together, something like:
Select c.courseid, c.coursename, p.prere1id, p.preqname
from course as c
left join prereq as p
on c.courseid = p.courseid
Related
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 4 years ago.
Improve this question
i have 3 tables which i want to combine in a empty table,
Table A contains:
a_id | name
1 | john
2 | mic
3 | rog
and
Table B contains:
b_id | name
10 | rims
11 | sara
and
Table c contains:
c_id | name
20 | johny
21 | sun
22 | rose
23 | pash
24 | ed
25 | ese
and i have one empty table D, which will have id's of all three above tables:
Table D columns are;
a_id | b_id | c_id
how can i insert all id's in table D? and
when i run query.
Select*from table_D
it should show all id's from table(a,b,c).
Your question is rather vague, because you don't specify what d looks like. Let me assume that you was a Cartesian product of all ids. This seems like a reasonable assumption. Then:
insert into d (a_id, b_id, c_id)
select a.a_id, b.b_id, c.c_id
from a cross join b cross join c;
Here is a rextester demonstrating it.
Here's what you ask for:
INSERT d
SELECT a.id, b.id, c.id
FROM a CROSS JOIN b CROSS JOIN c
Though I doubt it's what you want!
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 5 years ago.
Improve this question
Example: I want to get all users who do not follow the id 2 user
Table Followers
id | user_id | follower_id
1 2 7
2 2 8
Table users
id | username | e-mail | group
Give this a shot:
Select users.*
from users
where users.id not in (
select followers.follower_id
from followers
where followers.user_id = "2"
)
I'm not a mysql expert but I believe a pseudo-sql would be:
select users.* from users where users.id is in (select distinct follower_id from followers where followers.user_id != 2)
The != means different.
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
I need to get a customer list with orders quantity. Actually use query below to get customers with orders like:
+-------+----------------+
| JAMES | 2 orders total |
| PAUL | 6 orders total |
+-------+----------------+
To do this I use this query:
SELECT *
FROM mod_users
INNER JOIN (SELECT order_user_id, count(*) as order_qty
FROM mod_orders
GROUP BY user_order_id) AS order_qty
ON mod_users.user_id = order_qty.order_user_id;
Now, I'd like to get users without orders too:
+-------+----------------+
| JAMES | 2 orders total |
| PAUL | 6 orders total |
| FRANK | 0 orders total |
+-------+----------------+
Can anyone help make query to get this?
Use LEFT JOIN instead of INNER JOIN:
SELECT mod_users.user_id, COALESCE(order_qty, 0) AS ordersCount
FROM mod_users
LEFT JOIN (SELECT order_user_id, count(*) as order_qty
FROM mod_orders
GROUP BY user_order_id) AS order_qty
ON mod_users.user_id = order_qty.order_user_id;
If mod_orders doesn't contain any records for a particular user, then order_qty will be NULL due to LEFT JOIN for this user. COALESCE converts this NULL value into 0.
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
i have here
table_A
| Customer ID | moneyspent |
001 50
002 30
003 20
003 20
002 30
i have seen a query that gets the sum of all moneyspent from table A
SELECT SUM(moneyspent) FROM table_A
but i want the results to be inserted in table B's column"totalspent" like this
table_B
| Customer ID | totalspent |
001 50
002 60
003 40
help pls.
thanks
Try this, it works I've checked:
INSERT INTO table_B (Customer_ID, totalspent)
(SELECT Customer_ID, sum(moneyspent) FROM table_A group by Customer_ID)
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
I think this is a really simple question but I can't crack it.
I have two tables in my mysql database, clubs_db and leagues_db.
clubs_db
id | name
1 | Club1
2 | Club2
3 | Club3
4 | Club4
5 | Club5
6 | Club6
leagues_db
id | team1 | team2 | team3 | team1_name | team2_name | team3_name |
1 | 1 | 2 | 3 | | | |
2 | 4 | 5 | 6 | | | |
All I want to do is insert the relevant club name into leagues_db from clubs_db.
I also want this to happen automatically when the values in leagues_db change.
Thanks if anybody can help me.
It sounds like you would be better served by dropping the teamN_name columns and using a view that joins the two tables together:
CREATE VIEW leagues_with_names AS
SELECT
l.id, l.team1, l.team2, l.team3,
t1.name AS team1_name,
t2.name AS team2_name,
t3.name AS team3_name
FROM leagues_db l
LEFT OUTER JOIN clubs_db t1 ON l.team1 = t1.id
LEFT OUTER JOIN clubs_db t2 ON l.team2 = t2.id
LEFT OUTER JOIN clubs_db t3 ON l.team3 = t3.id;
Then you can SELECT ... FROM leagues_with_names and not have to worry about the details of the join. Note that the view is not a table in itself; it will fetch data from the other two tables automatically. This means that it will be always up to date.
(See a demo of this query.)