I have two tables on my Database: Menu and Dishes. ( I've translated the columns and database name, don't bother if doesn't make sense )
Menu Table
id date_begins date_ends id_dishes_monday id_dishes_tuesday id_wednesday
1 xxxxx xxxxx 1 2 3
Dishes Table
id date dynamic_dishes
1 2016/03/02 BLOB
2 2016/03/03 BLOB
3 2016/03/04 BLOB
I wanted to SELECT the id_dishes_monday, id_dishes_tuesday, id_wednesday From Menu Table and retrieve multiple rows from Dishes Table.
I'm made an attempt using this QUERY but it return only one row and I don't know why.
SELECT D.* FROM Menu M INNER JOIN Dishes D WHERE D.id IN (M.id_dishes_monday,M.id_dishes_tuesday,id_dishes_wednesday) ORDER BY M.id DESC LIMIT 1
What this Query Produces in this Example:
id date dynamic_dishes
1 2016/03/02 BLOB
What I want:
id date dynamic_dishes
1 2016/03/02 BLOB
2 2016/03/03 BLOB
3 2016/03/04 BLOB
Note that the tables datas are just for exemplification, I'd like to know why this Query didn't worked as well the correct Query for it.
Because you are limiting your output!
SELECT D.*
FROM Menu M
INNER JOIN Dishes D
WHERE D.id IN (M.id_dishes_monday,M.id_dishes_tuesday,id_dishes_wednesday)
ORDER BY M.id DESC
I've deleted the LIMIT 1 at the end of your query, which limit the result to the first row
Related
I have 2 tables, countries and users
users
id
country_id
1
1
2
1
3
1
4
2
5
2
countries
id
monthly_count
1
0
2
0
I want to update the monthly_count column in countries table by counting rows in users table grouped by country_id in a single update query. So after updating the countries table will look like this:
countries
id
monthly_count
1
3
2
2
The query I ended up with is:
UPDATE countries c
SET c.monthly_count = (
SELECT COUNT(u.country_id) FROM users u
WHERE u.country_id = c.id
GROUP BY u.country_id
);
The query is working as expected. It is updating the monthly_count column of every row in the countries table with corresponding correct count values for each country_id group from users table.
However, I am not sure how the query is working. The main question I have is, how the result of subquery is assigned to the correct row? Is the subquery executed one time and returning all the group by count values for each country_id at once or, is the sub query being executed each time for every row in countries table?
I would write this an update join:
UPDATE countries c
INNER JOIN
(
SELECT country_id, COUNT(*) AS cnt
FROM users
GROUP BY country_id
) u
ON u.country_id = c.id
SET c.monthly_count = u.cnt;
That being said, the monthly counts are aggregate data, and you might want to consider not doing this update or storing this data. So, I am happy with the following query or view:
SELECT c.id, COUNT(u.cnt) AS monthly_count
FROM countries c
LEFT JOIN users u
ON u.country_id = c.id
GROUP BY c.id;
I need to make one SQL command.
From table with comments i'll get comment id, then
with this ID I need to get count of reactions with the same comment ID and user's names.
So for example I have this 2 tables:
Comments:
ID
Comm_text
1
Example text
2
Another example
and Reactions:
ID
comm_id
usr
etc..
1
1
Peter
another
2
1
John
collon
3
1
Dog
cuz
4
2
Cat
why not
I need to get this:
ID
Comm_text
Reactions_Count
Users
1
Example text
3
Peter, John, Dog
2
Another example
1
Cat
I tried this:
SELECT k.id, k.comm, COUNT(r.id) as reactions, r.usr
FROM `comms` k
INNER JOIN `reactions` r ON r.id=k.id
It's just one row with one comment and count of all rows in reaction table.
Thanks.
Try this query that makes the same output:
select comments.id as ID , comments.Comm_text as Comm_text ,
(select count(id) from Reactions where comm_id = comments.id) as Reactions_Count ,
(select coalesce(GROUP_CONCAT(usr ORDER BY usr DESC) , '') from Reactions WHERE comm_id = comments.id) as Users
from comments group by comments.id
You should use group by to group the comments and have just one row then use query to count and gather the data, based on each row of the group.
The GROUP_CONCAT attach the output with , and the coalesce set the output to a given string if the output was empty.
Read more about:
GROUP BY
GROUP_CONCAT
COALESCE
subquery
According to the names that u set in the example, this will work. Just fix the table names for your database structure.
SELECT `Comments`.`ID`, `Comments`.`Comm_text`, count(`Reactions`.`comm_id`) as react, `Reactions`.`usr`
FROM `Comments`
INNER JOIN `Reactions`
ON `Comments`.`ID`=`Reactions`.`comm_id`
GROUP BY `Reactions`.`comm_id`
SELECT art.*,arg. FROM rd_articles AS art
LEFT JOIN rd_argument AS arg ON art.cat=arg.id WHERE art.enabled=1 ORDER BY art.id DESC
LIMIT 10
This is simple join query
Article table structure is
ID cat Description Date
1 1 Abc 08-01-2014
2 1 Aaa 10-01-2014
3 2 Abcv 11-01-2014
4 3 Aaa 12-01-2014
5 3 Aaa 14-01-2014
Arguments table is
ID Name
1 A
2 B
3 C
I want pick last updated(Date) one item from each cat.
How ?
This assumes that the enabled column is in rd_articles:
SELECT art.*, arg.*
FROM (
SELECT * FROM rd_articles
INNER JOIN (
SELECT cat, MAX(date) AS maxdate
FROM rd_articles
WHERE enabled = 1
GROUP BY cat
) md ON rd_articles.cat = md.cat AND rd_articles.date = md.maxdate
) art
LEFT JOIN rd_argument AS arg ON art.cat = arg.id
The innermost query gets the maximum date for each category, then joins it to the rd_articles table to get only those rd_articles rows that have the latest date for each article. That becomes the cat alias, which is then left-joined to the arguments table just like in your original query. You can add the LIMIT 10 at the end if needed; I wasn't sure what to do with that.
Note that if there's a tie for a category's latest date, you'll get more than one row for each category. If a tie could happen you'll need to break the tie somehow, for example by using the description or the ID. Let me know if that's the case and I'll update my answer.
SELECT ART.*, ARG.*
FROM ARTICLE AS ART
INNER JOIN RD_AGRUEMENT AS ARG
ON ARG.ID = ART.ID
WHERE (ID, DATE) IN
(SELECT ID, MAX(DATE) FROM ARTICLE GROUP BY ID)
Im really struggling to get my head around what should be simple,
I have two tables, one contains records the other is a mapping table.
records
ID Title Description
1 record 1 desc 1
2 record 2 desc 2
3 record 3 desc 3
4 record 4 desc 4
mapping table
ID1 ID2
1 3
2 4
What I want to do is get the two titles of each row in the mapping table. So the above would output
record 1 record 3
record 2 record 4
Im missing something really obvious, trying multiple joins results in errors trying to link the same table twice.
The following returns NUll
SELECT records.title FROM mapping
LEFT JOIN records
ON mapping.ID1 = records.id
AND mapping.ID2 = records.id
try this one: (UNTESTED)
SELECT b.Title as TitleA,
c.Title as TitleB
FROM mapping a
INNER JOIN records b
on a.ID1 = b.ID
INNER JOIN records c
on a.ID2 = c.ID
I've been searching everywhere for this but no cigar. Smoke is starting to come out of my ears. please help
How do you sum two columns in two tables and group by userid?
Have two tables.
Recipe Table
recipeid userid recipe_num_views
Meals Table
mealsid userid meal_num_views
Goal is to sum the num views in both tables and group by userid
so for example
Recipe Table
1 3 4
2 4 6
Meal Table
1 3 2
2 4 5
select sum(recipe views)+sum(meal views)
WHERE recipe.userid=meals.userid GROUP BY userid
should give
userid=3 , sum=6
userid=4, sum=11
this gives a much bigger number.
SELECT recipe.userid, sum(recipe_num_views+meal_num_views)
FROM Recipe JOIN Meals ON recipe.userid=meals.userid
GROUP BY recipe.userid
EDIT:
OK, from your comments, I understand that when you have for user 3: 4 recipes & 3 meals you will get the sum of the combination of all these rows => sum(recipes)*3 + sum(meals)*4
Try this query instead:
select r.userid, (sum_recipe + sum_meal) sum_all
FROM
(select userid, sum(recipe_num_views) sum_recipe
FROM Recipe
GROUP BY userid) r
JOIN (
select userid, sum(meal_num_views) sum_meal
FROM Meals
GROUP BY userid) m ON r.userid = m.userid
If you're selecting from 2 tables you need to join them.
Otherwise MySQL will not know how to link up the two tables.
select sum(recipe_num_views + meal_num_views)
from recipe r
inner join meals m ON (r.user_id = m.user_id)
group by m.user_id
See:
http://dev.mysql.com/doc/refman/5.5/en/join.html
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html