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.
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 years ago.
Improve this question
I was trying to fetch data from following tables.
table_question
q_id | question
1 | q1
2 | q2
3 | q3
4 | q4
table_answer
a_id| answer
1 | a1
2 | a2
3 | a3
4 | a4
5 | a5
table_entity
e_id| q_id | a_id
1 | 1 | 1
2 | 1 | 3
3 | 2 | 2
4 | 2 | 4
5 | 3 | 5
6 | 4 | 2
I want a question and answer. Please give me some suggestion.
Fetche data by using doctorine 2 ORM.
Try this mysql with your phpmyadmin
$qry = "SELECT quetion.que,answer.ans FROM entity INNER JOIN quetion ON quetion.id=entity.q_id Inner Join answer On answer.id=entity.a_id";
There are use join table for join those table and get the data from those table
you need to use join query. With joining both table you can get all tables relational data
$query = SELECT table_question.question,table_answer.answer FROM table_entity INNER JOIN table_question ON table_question.id=table_entity.`q_id ` Inner Join table_answer On table_answer.id=table_entity.a_id ;
Here you go:
SELECT table_question.question, table_answer,answer FROM table_entity
JOIN table_question ON (table_entity.q_id = table_question.q_id)
JOIN table_answer ON (table_entity.a_id = table_answer.a_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 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 6 years ago.
Improve this question
I have a table that is Scoreboard.
userID|userName|weekID|pickID|Score
--------------------------------------------
1 |Bob | 1 | Den | 10
1 |Bob | 2 | Clev | 20
2 |Adam | 1 | Car | 12
2 |Adam | 2 | Den | 30
I would like to sort the table by the sum of all of the weeks
My desired result would be this.
userID|userName|weekID|pickID|Score
--------------------------------------------
2 |Adam | 1 | Car | 12
2 |Adam | 2 | Den | 30
1 |Bob | 1 | Den | 10
1 |Bob | 2 | Clev | 20
Adams entries are on top because the sum of his score for Week 1 and Week 2
is the highest. The whole table needs to be sorted this way, so while Im spitting out the HTML on a loop, the rows will be sorted in order.
You need to get the aggregate score per user and sort by that total in descending order, and optionally one or more other columns as well. I assume weekID is a good secondary sort.
You can JOIN the table to itself based on userID, GROUP BY the main table's userID and weekID, and SUM the JOINed table's Score column in order to be able to ORDER correctly.
SELECT s.userid, s.username, s.weekid, s.pickid, s.score, SUM(a.score) totalscore
FROM scoreboard s
INNER JOIN scoreboard a ON a.userid = s.userid
GROUP BY s.userid, s.weekid
ORDER BY totalscore DESC, s.weekid;
If you don't need the total in the data returned you can put the SUM directly in the ORDER BY clause.
SELECT s.userid, s.username, s.weekid, s.pickid, s.score
FROM scoreboard s
INNER JOIN scoreboard a ON a.userid = s.userid
GROUP BY s.userid, s.weekid
ORDER BY SUM(a.score) DESC, s.weekid;
I assume your data is more normalized than this, so adjust accordingly.
I also imagine that you plan on operating for more than just a single season, so you may eventually need to filter on that as well.
The "GROUP BY" ist the key. The following should work:
SELECT userID, SUM(Score) as TotalScore
FROM teh_table
GROUP BY userID
ORDER BY TotalScore DESC;
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.)
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 am really not good at math, but can someone help me though how i can get the place the user has on all entries? Etc. "You are now number 43 out of 20.403.044 entries"?
SELECT ?myPlace? FROM entries WHERE userid = 1 ORDER BY time ASC
ok, i think you want
SELECT COUNT(*) FROM TABLE WHERE id <= YOUR_USER_ID
You can get the rank with this query:
SELECT COUNT(*) + 1
FROM entries entries1
INNER JOIN entries entries2 ON (entries1.id != entries2.id AND entries2.time < entries1.time)
WHERE entries1.id = 4
You just need to count the number of people with a better time, and add 1 (since the first rank is 1).
Unfortunately MySQL lacks windowing functions. Therefore you have emulate them. One way to do it is something like this
SELECT userid,
(
SELECT 1 + COUNT(*)
FROM entries
WHERE time < e.time
) place
FROM entries e
WHERE userid = 1
Sample output:
| USERID | PLACE |
|--------|-------|
| 1 | 2 |
If you need total number of entries in the same query
SELECT userid,
(
SELECT 1 + COUNT(*)
FROM entries
WHERE time < e.time
) place,
(
SELECT COUNT(*)
FROM entries
) total
FROM entries e
WHERE userid = 1;
Sample output:
| USERID | PLACE | TOTAL |
|--------|-------|-------|
| 1 | 2 | 5 |
Here is SQLFiddle demo