I have a listing_categories table that has many listing_plans and listings, and many subcategories. I am trying to write a view to show the listing categories with listing_plan count, listings count and subcategory count.
Here is my listing_categories table:
MariaDB [railsapp_development]> describe listing_categories;
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| website_id | int(11) | YES | MUL | NULL | |
| listing_category_id | int(11) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| slug | varchar(255) | YES | | NULL | |
| description | text | YES | | NULL | |
| category_card | text | YES | | NULL | |
| listing_card | text | YES | | NULL | |
| layout | varchar(255) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| keywords | varchar(255) | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
And the view I am trying to write:
create or replace view `listing_category_details` AS
select
cats1.*,
count(l.id) as listing_count,
count(cats2.id) as subcategory_count,
count(lp.id) as plan_count
from
listing_categories as cats1
left join
listings as l on cats1.id = l.listing_category_id
left join
listing_plans as lp on cats1.id = lp.listing_category_id
inner join
listing_categories as cats2 on cats1.listing_category_id = cats2.id
group by
cats1.id
order by
cats1.name asc;
The output is incorrect, it is not showing the right count, as follows:
MariaDB [railsapp_development]> select id, slug, name, listing_count, subcategory_count, plan_count from listing_category_details limit 10;
+----+-------------------+-------------------+---------------+-------------------+------------+
| id | slug | name | listing_count | subcategory_count | plan_count |
+----+-------------------+-------------------+---------------+-------------------+------------+
| 17 | ares | Ares | 22 | 22 | 22 |
| 30 | automotive | Automotive | 16 | 16 | 16 |
| 19 | crist-osinski-inc | Crist-Osinski Inc | 12 | 12 | 12 |
| 29 | esl-cologne | ESL Cologne | 20 | 20 | 20 |
| 18 | executive-office | Executive Office | 22 | 22 | 22 |
| 27 | gfinity-london | GFinity London | 24 | 24 | 24 |
| 25 | hephaestus | Hephaestus | 28 | 28 | 28 |
| 24 | iem-championship | IEM Championship | 14 | 14 | 14 |
| 26 | league-all-stars | League All Stars | 30 | 30 | 30 |
| 21 | machinery | Machinery | 14 | 14 | 14 |
+----+-------------------+-------------------+---------------+-------------------+------------+
In the above example none of the figures are corect, for instance each listing_category has exactly 2 plans.
What am I doing wrong?
Use COUNT(DISTINCT):
select cats1.*,
count(distinct l.id) as listing_count,
count(distinct cats2.id) as subcategory_count,
count(distinct lp.id) as plan_count
from listing_categories cats1 left join
listings l
on cats1.id = l.listing_category_id left join
listing_plans lp
on cats1.id = lp.listing_category_id inner join
listing_categories cats2
on cats1.listing_category_id = cats2.id
group by cats1.id
order by cats1.name asc;
COUNT() just counts the number of non-NULL values. You are joining along a hierarchy, so the rows get multiplied as you descend the hierarchy.
Related
I need to use a data visualization tool that can only query a single source for a given chart. I have three tables with the data I need to visualize. So, I need to combine them into a single view or output table. Here are the table schemas:
MySQL [bdCaloriesNeeded]> desc activity;
+---------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | text | YES | | NULL | |
| Gender | text | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| length | text | YES | | NULL | |
| weight | int(11) | YES | | NULL | |
| exercise | int(11) | YES | | NULL | |
| food_consumed | int(11) | YES | | NULL | |
| date | datetime | YES | | NULL | |
+---------------+----------+------+-----+---------+-------+
MySQL [bdCaloriesNeeded]> desc exercise;
+---------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------+------+-----+---------+-------+
| Gender | text | YES | | NULL | |
| Min_Age | int(11) | YES | | NULL | |
| Max_Age | int(11) | YES | | NULL | |
| min_exercise_hours | int(11) | YES | | NULL | |
| med_exercise_hours | int(11) | YES | | NULL | |
| high_exercise_hours | int(11) | YES | | NULL | |
+---------------------+---------+------+-----+---------+-------+
MySQL [bdCaloriesNeeded]> desc food;
+---------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------+------+-----+---------+-------+
| size | text | YES | | NULL | |
| min_pounds | int(11) | YES | | NULL | |
| max_pounds | int(11) | YES | | NULL | |
| min_food_oz_per_day | int(11) | YES | | NULL | |
| max_food_oz_per_day | int(11) | YES | | NULL | |
+---------------------+---------+------+-----+---------+-------+
Here's the actual source data in the above tables:
MySQL [bdCaloriesNeeded]> select * from activity;
+------+----------+--------+------+--------+--------+----------+---------------+---------------------+
| id | name | Gender | age | length | weight | exercise | food_consumed | date |
+------+----------+--------+------+--------+--------+----------+---------------+---------------------+
| 14 | spot | M | 2 | 2'7" | 13 | 5 | 13 | 2017-10-08 00:00:00 |
| 67 | princess | F | 6 | 3'3" | 75 | 3 | 15 | 2017-09-05 00:00:00 |
+------+----------+--------+------+--------+--------+----------+---------------+---------------------+
MySQL [bdCaloriesNeeded]> select * from exercise
+--------+---------+---------+--------------------+--------------------+---------------------+
| Gender | Min_Age | Max_Age | min_exercise_hours | med_exercise_hours | high_exercise_hours |
+--------+---------+---------+--------------------+--------------------+---------------------+
| M | 1 | 2 | 1 | 4 | 6 |
| M | 3 | 7 | 1 | 3 | 4 |
| M | 8 | 15 | 1 | 2 | 2 |
| F | 1 | 2 | 1 | 4 | 6 |
| F | 3 | 7 | 1 | 3 | 5 |
| F | 8 | 15 | 1 | 2 | 2 |
+--------+---------+---------+--------------------+--------------------+---------------------+
MySQL [bdCaloriesNeeded]> select * from food;
+--------+------------+------------+---------------------+---------------------+
| size | min_pounds | max_pounds | min_food_oz_per_day | max_food_oz_per_day |
+--------+------------+------------+---------------------+---------------------+
| small | 1 | 10 | 12 | 18 |
| medium | 11 | 30 | 15 | 30 |
| large | 31 | 100 | 25 | 50 |
+--------+------------+------------+---------------------+---------------------+
Here's the SQL I'm executing:
SELECT activity.id, activity.name, activity.Gender, activity.age, activity.weight, activity.exercise, activity.date, exercise.min_exercise_hours, exercise.high_exercise_hours, food.size, food.min_food_oz_per_day, food.max_food_oz_per_day
from activity, exercise, food
where (
activity.exercise between exercise.min_exercise_hours and exercise.high_exercise_hours
)
and
(
activity.weight between food.min_pounds and food.max_pounds
)
and
(
activity.Gender = exercise.Gender
)
Here's the undesired result I'm getting:
+------+----------+--------+------+--------+----------+---------------------+--------------------+---------------------+--------+---------------------+---------------------+
| id | name | Gender | age | weight | exercise | date | min_exercise_hours | high_exercise_hours | size | min_food_oz_per_day | max_food_oz_per_day |
+------+----------+--------+------+--------+----------+---------------------+--------------------+---------------------+--------+---------------------+---------------------+
| 14 | spot | M | 2 | 13 | 5 | 2017-10-08 00:00:00 | 1 | 6 | medium | 15 | 30 |
| 67 | princess | F | 6 | 75 | 3 | 2017-09-05 00:00:00 | 1 | 6 | large | 25 | 50 |
| 67 | princess | F | 6 | 75 | 3 | 2017-09-05 00:00:00 | 1 | 5 | large | 25 | 50 |
+------+----------+--------+------+--------+----------+---------------------+--------------------+---------------------+--------+---------------------+---------------------+
I'm getting two rows for Princess. I need one row for each dog. The desired result should use Princess's's weight to look up the correct range of food per day, and use her gender and age to look up the correct range of exercise.
I've been banging on this for hours, can't see what doing wrong here.
So interestingly your question says that the tables are Unrelated but they are actually related and this is the whole point of a relational database, to join data based on those relationships.
The issue is that your exercise table is only being joined on exercise hours using the between so princess matches rows 4 and 5 in the exercise table. (the first where clause matches rows 1 and 2 also but the later where clause limits the Gender)
It looks to me like you should also limit the match on the exercise table to age as well as exercise and gender
so add
and (activity.age between exercise.min_age and exercise.max_age)
Also personally i like to use JOIN clauses rather than WHERE - it keeps all the stuff together.
SELECT activity.id,
activity.name,
activity.Gender,
activity.age,
activity.weight,
activity.exercise,
activity.date,
exercise.min_exercise_hours,
exercise.high_exercise_hours,
food.size,
food.min_food_oz_per_day,
food.max_food_oz_per_day
FROM activity
JOIN exercise
ON activity.exercise BETWEEN exercise.min_exercise_hours AND exercise.high_exercise_hours
AND activity.Gender = exercise.Gender
AND activity.age BETWEEN exercise.min_age AND exercise.max_age
JOIN food
ON activity.weight BETWEEN food.min_pounds AND food.max_pounds
Since you are looking for things that may be OUTSIDE of the ranges suggested you may want to consider LEFT JOIN on the exercise and food tables, so that the dogs on the activity table that fall outside of any range will still show up (with NULL values for the missing data for the other table.)
just change the join lines to LEFT JOIN like so:
LEFT JOIN exercise
LEFT JOIN food
See also: What is the difference between "INNER JOIN" and "OUTER JOIN"?
This query
SELECT station_id, station_name,
COUNT(event_station) as `total_visit_count`
FROM taps AS t
JOIN event_stations AS s
ON t.event_station = s.station_id
WHERE s.event_id=6
GROUP BY s.station_id
ORDER BY s.station_id;
returns
+------------+--------------+-------------------+
| station_id | station_name | total_visit_count |
+------------+--------------+-------------------+
| 5 | Station one | 24 |
| 6 | Station two | 35 |
| 7 | St. Pancras | 34 |
+------------+--------------+-------------------+
which is just fine.
However, there are some stations in taps which have not been visited and I would like them to be shown with a total_visit_count of zer0.
+------------+--------------+-------------------+
| station_id | station_name | total_visit_count |
+------------+--------------+-------------------+
| 5 | Station one | 24 |
| 6 | Station two | 35 |
| 7 | St. Pancras | 34 |
| 8 | Station four | 0 |
+------------+--------------+-------------------+
How do I rewrite my query to to that? I imagine some kind of JOIN is required, but I can't quite see it :-(
[Update]
describe event_Stations;
+--------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+----------------+
| station_id | int(11) | NO | PRI | NULL | auto_increment |
| event_id | int(11) | NO | | NULL | |
| station_name | text | NO | | NULL | |
| allocated | tinyint(1) | NO | | 0 | |
+--------------+------------+------+-----+---------+----------------+
4 rows in set (0.20 sec)
describe taps;
+---------------+-----------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-----------+------+-----+-------------------+-------+
| tag_id | int(11) | NO | | NULL | |
| time_stamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| event_station | int(11) | NO | | NULL | |
| device_id | text | YES | | NULL | |
| device_type | text | YES | | NULL | |
| event_id | int(11) | NO | | NULL | |
+---------------+-----------+------+-----+-------------------+-------+
6 rows in set (0.00 sec)
select * from event_stations where event_id=6;
+------------+----------+-----------------+-----------+
| station_id | event_id | station_name | allocated |
+------------+----------+-----------------+-----------+
| 5 | 6 | Station one | 0 |
| 6 | 6 | Station two | 0 |
| 7 | 6 | St. Pancras | 0 |
| 8 | 6 | Station three | 0 |
| 9 | 6 | Station four | 0 |
| 10 | 6 | Station five | 0 |
| 11 | 6 | Station six | 0 |
| 12 | 6 | Station seven | 0 |
| 13 | 6 | Station eight | 0 |
| 14 | 6 | Station nine | 0 |
| 15 | 6 | Station ten | 0 |
| 16 | 6 | Station eleven | 0 |
+------------+----------+-----------------+-----------+
12 rows in set (0.00 sec)
First, swap the order of your join, so the primary table is sorted first (this is for organizational purposes only).
Then, use a LEFT JOIN to accomplish what you're looking for. This will ensure you pull all event_stations records (the left portion of the join), even if there is no corresponding record in the taps table (the right portion of the join). In place of the missing taps, you'll get NULL values.
COUNT will ignore nulls in aggregate, so will only return the count of non-null records. Thus, it will return 0 for your missing event_stations records.
SELECT
station_id,
station_name,
COUNT(event_station) as `total_visit_count`
FROM event_stations AS s
LEFT JOIN taps AS t
ON t.event_station = s.station_id
WHERE s.event_id = 6
GROUP BY s.station_id
ORDER BY s.station_id;
Alternatively, you could just use a RIGHT JOIN with your original join order. I personally don't like doing that, though, because I'm a LTR reader (first in order is more important).
I have these two tables
facilities
+----+--------------------------------------+--------------+-------+------+------------+------------+
| id | name | category | image | cust | created_at | updated_at |
+----+--------------------------------------+--------------+-------+------+------------+------------+
| 1 | Bar | hotel | | | NULL | NULL |
| 2 | Minibar in alle Zimmer | hotel | | | NULL | NULL |
| 3 | Snack restaurant | hotel | | | NULL | NULL |
| 4 | Grillroom | hotel | | | NULL | NULL |
+----+--------------------------------------+--------------+-------+------+------------+------------+
table - 2
resortfacilities
+----+------+---------------------+---------------------+------+
| id | f_id | created_at | updated_at | h_id |
+----+------+---------------------+---------------------+------+
| 42 | 4 | 2016-09-21 13:17:27 | 2016-09-21 13:17:27 | 35 |
| 59 | 1 | 2016-09-22 10:23:27 | 2016-09-22 10:23:27 | 38 |
| 60 | 4 | NULL | NULL | 38 |
+----+------+---------------------+---------------------+------+
And I have ran this following sql
SELECT
facilities.id,
facilities.`name`,
facilities.image,
resortfacilities.h_id
FROM
facilities
LEFT JOIN resortfacilities ON facilities.id = resortfacilities.f_id
WHERE
resortfacilities.f_id IS NULL AND
facilities.category = 'hotel' OR
resortfacilities.h_id <> 35
with the expectation of get getting the following result
+----+---------------------------+-------+------+
| id | name | image | h_id |
+----+---------------------------+-------+------+
| 4 | Grillroom | | 38 |
| 2 | Minibar in alle Zimmer | | NULL |
| 3 | Snack restaurant | | NULL |
+----+---------------------------+-------+------+
with out the values that belongs to h_id 35 but I end up with
+----+---------------------------+-------+------+
| id | name | image | h_id |
+----+---------------------------+-------+------+
| 1 | Bar | | 38 |
| 4 | Grillroom | | 38 |
| 2 | Minibar in alle Zimmer | | NULL |
| 3 | Snack restaurant | | NULL |
+----+---------------------------+-------+------+
May I know how to get the that result?
use the below script (for getting the result as mentioned ).
SELECT
f.id,
f.`name`,
f.image,
r.h_id
FROM
facilities f
LEFT JOIN resortfacilities r ON f.id = f.f_id AND r.h_id <> 35
WHERE f.category = 'hotel'
The following MySQL queries below are returning results in different orders despite the "ORDER BY" being the same for both queries:
TABLE STRUCTURE
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| image_id | int(10) | NO | PRI | NULL | auto_increment |
| property_id | int(10) | NO | | 0 | |
| image_title | text | NO | | NULL | |
| image_title_id | int(10) | NO | | 0 | |
| image_index | smallint(3) | NO | | 0 | |
| image_version | tinyint(1) | NO | | 2 | |
| image_landscape | tinyint(1) | NO | | 1 | |
| image_visible | tinyint(1) | NO | | 1 | |
| image_type | tinyint(1) | NO | | 3 | |
+-----------------+-------------+------+-----+---------+----------------+
TEST 1
Query:
SELECT image_id, room_text
FROM property_record_images
INNER JOIN property_data_rooms ON property_record_images.image_title_id = property_data_rooms.room_id
WHERE property_id = 1029
ORDER BY image_index
Result:
+----------+-----------------+
| image_id | room_text |
+----------+-----------------+
| 2042 | Front elevation |
| 2043 | Garden to rear |
| 2044 | Kitchen |
| 2045 | Breakfast area |
| 2046 | Lounge |
| 2047 | Master bedroom |
| 2048 | Studio |
+----------+-----------------+
TEST 2
Query:
SELECT GROUP_CONCAT(CONCAT(property_record_images.image_id) SEPARATOR '|')
FROM property_record_images
INNER JOIN property ON property_record_images.property_id = property.property_id
WHERE property_record_images.property_id = 1029
ORDER BY image_index
Result:
+---------------------------------------------------------------------+
| GROUP_CONCAT(CONCAT(property_record_images.image_id) SEPARATOR '|') |
+---------------------------------------------------------------------+
| 2048|2047|2044|2045|2046|2043|2042 |
+---------------------------------------------------------------------+
This is occurring with random records (different "property_id") so it's not an simple as just reversing the ORDER BY for the second query.
Any idea why this is happening and where I have gone wrong with the query?
see http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
I think you should get ordered group concat by:
SELECT GROUP_CONCAT(CONCAT(property_record_images.image_id) ORDER BY image_index SEPARATOR '|')
FROM property_record_images
INNER JOIN property ON property_record_images.property_id = property.property_id
WHERE property_record_images.property_id = 1029
ORDER BY image_index
I have a people table, a units table, and a wellness table (described below). Basically at my work we have to check up on people, and if they haven't been seen for two days we go looking for them... What I'd like is to be able to select all records from the wellness table that are older than two days, but only the NEWEST ones (as there will probably be multiple entries per person per day, which is how I want it because with the wellness.username field you can tell who saw who, and when).
People
+------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fname | varchar(32) | NO | | NULL | |
| lname | varchar(32) | NO | | NULL | |
| dob | date | NO | | 0000-00-00 | |
| license_no | varchar(24) | NO | | NULL | |
| date_added | timestamp | NO | | CURRENT_TIMESTAMP | |
| status | varchar(8) | NO | | Allow | |
+------------+-------------+------+-----+-------------------+----------------+
Units
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| number | varchar(3) | NO | | NULL | |
| resident | int(11) | NO | MUL | NULL | |
| type | varchar(16) | NO | | NULL | |
+----------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+-------------------+----------------+
| wellness_id | int(11) | NO | PRI | NULL | auto_increment |
| people_id | int(11) | NO | | NULL | |
| time_checked | timestamp | NO | | CURRENT_TIMESTAMP | |
| check_type | varchar(1) | NO | | NULL | |
| username | varchar(16) | NO | | jmd9qs | |
+--------------+-------------+------+-----+-------------------+----------------+
The units table is ugly, I know, but it will be changed soon; as it stands, resident references people.id.
Here's my Minimum Working Example, which only gives me one result even though there are multiple people in wellness that have a time_checked row older than 2 days. The one result I do get is more like 4-1/2 days old....
select w.wellness_id, p.id, p.lname, p.fname, u.number, u.type,
w.time_checked, w.check_type, w.username
from people p
left join units u on p.id = u.resident
right join wellness w on p.id = w.people_id
WHERE w.time_checked <= DATE_ADD(CURDATE(), INTERVAL -2 DAY)
order by w.time_checked asc;
I'm trying to get the newest records that are older than two days, but only 1 per people that are in the wellness table. I join all of the other stuff because I need it for displaying records with PHP.
Sorry for my rather rambling question, hope it's clear enough!
EDIT - Sample Data:
+------+------+--------+---------------------+------------+----------+
| w_id | id | number | time_checked | check_type | username |
+------+------+--------+---------------------+------------+----------+
| 100 | 2 | 425 | 2013-08-23 21:03:00 | s | jmd9qs |
| 101 | 2 | 425 | 2013-08-25 05:41:01 | s | jmd9qs |
| 91 | 2 | 425 | 2013-08-20 19:52:23 | s | jmd9qs |
| 83 | 4 | 416 | 2013-08-23 20:12:29 | s | jmd9qs |
| 76 | 5 | 408 | 2013-08-23 20:11:21 | s | jmd9qs |
| 62 | 6 | 327 | 2013-08-23 20:06:13 | s | jmd9qs |
| 18 | 7 | 204 | 2013-08-23 19:43:58 | s | jmd9qs |
| 31 | 8 | 219 | 2013-08-23 19:51:11 | s | jmd9qs |
| 97 | 9 | 432 | 2013-08-23 20:16:39 | o | jmd9qs |
| 44 | 10 | 309 | 2013-08-23 19:55:45 | s | jmd9qs |
+------+------+--------+---------------------+------------+----------+
IMHO RIGHT JOIN from people to wellness makes no sense since there shouldn't be a situation when you have a person in wellness but don't have that person in people table. But vice versa is true. You have a new person but don't have any wellness info about him or her yet.
That being said your query might look like this
SELECT z.wellness_id, p.id, z.time_checked, z.check_type, z.username
FROM people p JOIN units u
ON p.id = u.resident LEFT JOIN
(
SELECT w.*
FROM
(
SELECT people_id, MAX(time_checked) time_checked
FROM wellness
GROUP BY people_id
) q JOIN wellness w
ON q.people_id = w.people_id
AND q.time_checked = w.time_checked
) z
ON p.id = z.people_id
WHERE COALESCE(time_checked, 0) < CURDATE() - INTERVAL 2 DAY
Here is SQLFiddle demo
Here's the query that gets what I want:
select p.id, u.number,
w.time_checked, w.check_type, w.username
from people p inner join units u on p.id = u.resident
left join wellness w on p.id = w.people_id
left outer join wellness as w2 ON w.people_id = w2.people_id
and w.time_checked < w2.time_checked
where w2.people_id is null and w.time_checked < (NOW() - INTERVAL 2 DAY)
order by w.time_checked asc