I have this 3 tables:
Users:
user_id|user_nick
1 | a
2 | b
Category:
cat_id|cat_type
1 | a
2 | b
3 | c
4 | d
Meta:
met_id|met_name|met_user|met_type
10 | bla | 1 | 1
11 | blabla | 2 | 2
12 | foo | 1 | 3
13 | blafoo | 2 | 4
14 | foofoo | 1 | 4
15 | foobla | 1 | 4
How can I return something like this ?
user_id|met_type|total
1 | 1 | 1
1 | 2 | 0
1 | 3 | 1
1 | 4 | 2
For just one user and not for all of them.
met_type is a foreign key from Category.
I've tried like this but no success :/
SELECT met_user, met_type, COUNT(*) FROM Meta GROUP BY met_user WHERE met_user = '1'
Query:
SELECT met_user, met_type, count(*)
FROM Meta
WHERE met_user='1'
GROUP BY met_type;
To get empty groups, you can use generateSeries() here:
SELECT m.met_user, g.meta_type, count(m)
FROM generate_series(1, 4) AS g(meta_type)
LEFT OUTER JOIN Meta AS m
ON m.met_user='1'
AND m.met_type=g.meta_type
GROUP BY g.meta_type, m.met_user
ORDER BY g.meta_type;
Check it out! I made an sql fiddle.
Related
How to select rows which are not defined? Like row 2 have undefined day 3 and row 3 have undefined day 1. I want them to be 0 in result set.
+----+-----+-------+
| id | day | count |
+----+-----+-------+
| 1 | 1 | 262 |
| 1 | 2 | 685 |
| 1 | 3 | 984 |
| 2 | 1 | 692 |
| 2 | 2 | 962 |
| 3 | 2 | 355 |
| 3 | 3 | 741 |
+----+-----+-------+
EDIT:
I want select count from days 1, 2 and 3 (not whole table) and display 0 on undefined day.
We can get all unique id values in a Derived Table.
For day, you seem to want only 1,2 and 3 only. So we can directly consider these values only using UNION ALL.
CROSS JOIN between them to get all possible combinations.
LEFT JOIN from all_combinations table to the main table on id and day.
We can use Coalesce() function to consider 0 value for count, for the cases where there is no matching row in the main table
Try the following:
SELECT all_combinations.id,
all_combinations.day,
COALESCE(t.count, 0) AS count
FROM
(
SELECT ids.id, days.day
FROM
(SELECT DISTINCT id FROM your_table) AS ids
CROSS JOIN
(SELECT 1 AS day UNION ALL SELECT 2 UNION ALL SELECT 3) AS days
) AS all_combinations
LEFT JOIN your_table AS t
ON t.id = all_combinations.id AND
t.day = all_combinations.day
Result:
| id | day | count |
| --- | --- | ----- |
| 1 | 1 | 262 |
| 2 | 1 | 692 |
| 3 | 1 | 0 |
| 1 | 2 | 685 |
| 2 | 2 | 962 |
| 3 | 2 | 355 |
| 1 | 3 | 984 |
| 2 | 3 | 0 |
| 3 | 3 | 741 |
View on DB Fiddle
I'm not sure if it's possible to accomplish this with mysql query alone, but I have a table in the following format:
+----+-----------+-----------------+-------+
| id | recipe_id | ingredient_id | order |
+----+-----------+-----------------+-------|
| 1 | 1 | 26 | 2 |
+----+-----------+-----------------+-------|
| 2 | 1 | 95 | 1 |
+----+-----------+-----------------+-------|
| 3 | 1 | 42 | 3 |
+----+-----------+-----------------+-------|
| 4 | 2 | 22 | 1 |
+----+-----------+-----------------+-------|
| 5 | 2 | 15 | 2 |
+----+-----------+-----------------+-------|
| 6 | 2 | 1 | 3 |
+----+-----------+-----------------+-------|
| 7 | 3 | 95 | 1 |
+----+-----------+-----------------+-------|
| 8 | 3 | 33 | 2 |
+----+-----------+-----------------+-------|
| 9 | 3 | 23 | 3 |
+----+-----------+-----------------+-------|
I wanted to generate a list of all recipe_id that have a consecutive order numbers. ex: 1, 2, 3, and so on, (recipe_id 2 for example) but if the they're not, for example, recipe_id 1 has 2, 1, 3, so it's not in order.
I'm using php, but I wanted to know if this was possible with mysql alone first.
You can use this to get all recipes where the order numbers not increasing with the id
SELECT * FROM mytable AS a, mytable AS b
WHERE ((a.id < b.id AND a.order > b.order) OR (a.id > b.id AND a.order < b.order)) AND a.recipe_id = b.recipe_id
And this one to get all recipes with there orders as one row
SELECT recipe_id, MAX(`order`)-MIN(`order`)+1=COUNT(`order`), GROUP_CONCAT(`order`)
FROM mytable GROUP BY recipe_id
Its only a beginning but i hope it will help you
I create a fiddle https://www.db-fiddle.com/f/dakAHH5YXt3o1rahXFznLy/0
id | rem_id |max_val
-- | ------ |------
1 | 1 | 7
2 | 2 | 6
3 | 3 | 1
4 | 1 | 1
5 | 2 | 1
6 | 3 | 1
In the above table I need to remove the duplicates from the rem_id column with min val in the max_val column
id | rem_id |max_val
-- | ------ |------
1 | 1 | 7
2 | 2 | 6
3 | 3 | 1
This will delete all but the highest valued max_val for each rem_id, or multiples of the same max_val by deleting those with a higher id:
delete t
from t
left join t as i
on i.rem_id = t.rem_id
and (i.max_val > t.max_val
or (i.max_val = t.max_val and i.id < t.id)
)
where i.id is not null;
rextester demo: http://rextester.com/QKIK17666
returns:
+----+--------+---------+
| id | rem_id | max_val |
+----+--------+---------+
| 1 | 1 | 7 |
| 2 | 2 | 6 |
| 3 | 3 | 1 |
+----+--------+---------+
I have two mysql tables.
table-1: table-item:
id | itemid | itemname | catid
---------------------------------
1 | 1 | Pen | 1
2 | 2 | Pencil | 1
3 | 3 | Sharpner | 1
4 | 4 | Book | 2
5 | 5 | Khata | 2
6 | 6 | Bag | 3
7 | 7 | File | 3
---------------------------------
table-2: yearly-item:
id | itemid |catid| year
-----------------------------
1 | 1 | 1 | 2015
2 | 3 | 1 | 2015
3 | 4 | 2 | 2015
4 | 6 | 3 | 2015
5 | 1 | 2 | 2016
6 | 1 | 1 | 2016
------------------------------
I want to get a list of items of catid-1 from table-item for the year-2016 which is not present in yearly-item table.
id | itemid | itemname | catid
---------------------------------
2 | 2 | Pencil | 1
3 | 3 | Sharpner | 1
---------------------------------
for that purpose, while executing this query:
SELECT * FROM table_item t LEFT JOIN yearly_item y ON t.itemid=y.itemid AND t.catid=y.catid WHERE t.catid=1 AND y.year='2016' AND y.itemid IS NULL
it is giving this result:
MySQL returned an empty result set (i.e. zero rows).
Try
select b.itemid, b.itemname, b.catid, a.year
from yearly_item a, item b
where year = 2016
and b.itemid != a.itemid
and b.catid = 1
and a.catid = 1
http://sqlfiddle.com/#!9/cfd66/14/0
I have this 2 tables:
Category:
cat_id|cat_type
1 | a
2 | b
3 | c
4 | d
and have
Meta:
met_id|met_name|user_id|met_type
10 | bla | 2 | 1
11 | blabla | 4 | 2
12 | foo | 1 | 3
13 | blafoo | 3 | 4
14 | foofoo | 5 | 4
How can I return something like this ?
cat_type|occurences
a | 1
b | 1
c | 1
d | 2
met_type is a foreign key from Category.
SELECT c.cat_type
, COUNT(m.met_type) AS occurrences
FROM categoty c LEFT JOIN meta m ON c.cat_id = m.met_type
GROUP BY c.cat_type
SELECT cate.cat_type as 'cat_id', count(meta.met_type) as 'occurences'
FROM Category cate
LEFT JOIN Meta meta on(cate.cat_id = meta.met_type)
GROUP BY meta.met_type