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"?
Related
This question already has answers here:
Select all where [first letter starts with B]
(6 answers)
Closed last year.
I am new to MySQL and am not very familiar with it. I am supposed to find a name that starts with b in index, which i do not have the slightest clue of doing.
| id | name | DoB | class | marks | dept_id |
+----+--------------+------------+-------+-------+---------+
| 1 | Data Science | 2006-07-15 | 11 | 100 | 3 |
| 2 | Garry | 2006-08-20 | 11 | 92 | 4 |
| 3 | Jane | 2006-03-22 | 10 | 95 | 2 |
| 4 | Benny | 2005-10-10 | 12 | 74 | 4 |
| 5 | Karen | 2005-01-15 | 12 | 88 | 3 |
| 6 | Camy | 2006-04-18 | 12 | 91 | 2 |
| 7 | Farhan | 2006-09-21 | 11 | 80 | NULL |
| 8 | Shamil | 2005-10-19 | 11 | 90 | 3 |
+----+--------------+------------+-------+-------+---------+
The Table above is (students)
And the one Below it is the Index of (students)
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| students | 0 | PRIMARY | 1 | id | A | 6 | NULL | NULL | | BTREE | | | YES | NULL |
| students | 1 | dept_id | 1 | dept_id | A | 3 | NULL | NULL | YES | BTREE | | | YES | NULL |
| students | 1 | name_index | 1 | name | A | 6 | NULL | NULL | | BTREE | | | YES | NULL |
| students | 1 | index_name | 1 | name | A | 6 | NULL | NULL | | BTREE | | | YES | NULL |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
You have to use SELECT query with LIKE keyword
The percentage ( % ) wildcard matches any string of zero or more characters. For example, b% matches any string starts with the character b such as Benny and Ben.
SELECT * FROM students WHERE name LIKE 'b%';
If you have same name in record which starts from uppercase as well as lowecase then put BINARY keyword after LIKE keyword (Case Sensitive):
SELECT * FROM students WHERE name LIKE BINARY 'b%';
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.
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).
Please someone help.
I have two tables, car_table and add_table, they are completely different tables.
car_table:
+---------+-------------+------+-----+------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+------------------+----------------+
| id | int(3) | NO | PRI | NULL | auto_increment |
| car | varchar(15) | NO | | NULL | |
| manuf | varchar(10) | NO | | NULL | |
|timestamp| timestamp | NO | | CURRENT_TIMESTAMP| |
+-------+-------------+------+-----+---------+---------------------------+
Sample Data:
+----+--------+-------+-----------+
| id | car | manuf | timestamp |
+----+--------+-------+-----------+
| 1 | M5 | BMW | time |
| 2 | Golf | VW | time |
| 3 | Toyota |pickup | time |
| 4 | Ford | focus | time |
+----+--------+-------+-----------+
add_table
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
|add_id | int(3) | NO | PRI | NULL | auto_increment |
| link | varchar(15) | YES | | NULL | |
| s_img | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
Sample Data:
+-------+--------+-------+
|add_id | link | s_img |
+-------+--------+-------+
| 1 | link | img |
| 2 | link | img |
| 3 | link | img |
| 4 | link | img |
+-------+--------+-------+
I am trying to write a select statement that will order the car_table by timestamp and limit the add_table to 3 results, mixing the results so that a add_table results are separated by car_table results.
Below is what I have so far but not sure where to go from here. (update: this seems to partially work, however not sure what it is ordering the add_table by and not sure if it's even the correct way of doing it.)
SELECT id,car,manuf,timestamp,
FROM car_table
UNION ALL
SELECT add_id,link,s_img,NULL AS timestamp
FROM add_table ORDER BY TimeStamp
I have a DB that has a bunch of businesses and the relationships between them. I am trying to find a way to get all Businesses that B2 sells to that B1 does not sell too. But Only on B2 where B1 and B2 sell to the same business.
V= Vendor, C = Client, Both are businesses but thinking in terms of Vendor/Client makes this easier to explain.
V1 -sells to-> C1 <- sells to- V2 -sells to-> C2
I am looking for all C2 that are not also in C1 for a specific V1 starting point.
My current tables:
mysql> DESCRIBE business;
+---------------+-------------------+------+-----+----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------------+------+-----+----------+----------------+
| business_id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(255) | YES | | | |
| name | varchar(255) | NO | | NULL | |
| city | varchar(255) | YES | | | |
| state | varchar(255) | YES | MUL | | |
| cCount | int(10) unsigned | YES | MUL | 0 | |
| scCount | int(10) unsigned | YES | MUL | 0 | |
| vCount | int(10) unsigned | YES | MUL | 0 | |
| svCount | int(10) unsigned | YES | MUL | 0 | |
+---------------+-------------------+------+-----+----------+----------------+
mysql> DESCRIBE relation_sells_to;
+---------+----------+------+-----+---------+--------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+--------+
| start | int(11) | NO | MUL | NULL | |
| end | int(11) | NO | MUL | NULL | |
+---------+----------+------+-----+---------+--------+
mysql> DESCRIBE vcvc;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| vendor | int(11) | NO | MUL | NULL | |
| client | int(11) | NO | | NULL | |
| vendor2 | int(11) | NO | | NULL | |
| client2 | int(11) | NO | | NULL | |
+---------+---------+------+-----+---------+-------+
I can use these two queries to get up to date results, But I have to drop the vcvc table every time I want to update it and it currently takes 15 min to build it. I also will not need access to all that info at any one time. I may need to generate theses lists for a few thousand at a time but not the full DB.
CREATE TABLE IF NOT EXISTS vcvc (INDEX vendor (vendor), INDEX client2 (client2))
SELECT r.start AS vendor, r.end AS client, r2.start AS vendor2, r3.end AS client2
FROM relation_sells_to AS r
JOIN relation_sells_to as r2 ON r.end = r2.end
JOIN relation_sells_to AS r3 ON r2.start = r3.start
WHERE r.start != r2.start
AND r.end != r3.end;
SELECT DISTINCT(client2), cCount, scCount, vCount, svCount
FROM vcvc
JOIN business AS b ON client2=b.business_id
WHERE vendor = ####
AND client2 NOT IN (SELECT client FROM vcvc WHERE vendor = ####)
ORDER BY cCount DESC;
Is there a way to do this in a single query so I dont have to build a whole new table every time I need to get this information? I currently have 500k relations and when I build this table I end up with over 100 million rows and most of those I don't need. Ideally I would be able to take an email(preferred) or business_id as the starting business/vendor, then just return a list of Business/clients with name, city, state, cCount, vCount and have them order in DESC order by vCount and probably LIMIT 20ish. Speed isn't a huge issue I really just want to be able to get current results without having to build a large table of which I need 20 of the 100+millions rows on it. I originally tried working with a temp table but because I cant "reopen" the temp table like my current query needs.
Thanks for the help.
Update with sample data and desired output.
Some of the cCount and vCounts might be off, I copied real data then modified it but I don't know if I updated all the numbers correctly as I added the required connections to show what I need.
mysql> SELECT * FROM business;
+-------------+----------------+----------------------+----------------+------------+--------+---------+--------+---------+
| business_id | email | name | city | state | cCount | scCount | vCount | svCount |
+-------------+----------------+----------------------+----------------+------------+--------+---------+--------+---------+
| 1 | bob#bob.com | Bobs Construction | Virginia Beach | Virginia | 62 | 3 | 0 | 0 |
| 2 | sue#bob.com | Upholstery by Sue | Austin | Texas | 20 | 3 | 4 | 4 |
| 3 | jim#bob.com | Jim & Associates | Crowley | Texas | 5 | 3 | 0 | 0 |
| 4 | jon#bob.com | Jon Jon architects | Costa Mesa | California | 67 | 3 | 0 | 0 |
| 5 | joe#bob.com | Joes Pizza | Hamden | Conecticut | 7 | 1 | 0 | 0 |
| 6 | tim#bob.com | Tims WIndows | Miami | Florida | 10 | 2 | 0 | 0 |
| 7 | ron#bob.com | Rons Hot Rods | Costa Mesa | California | 8 | 4 | 0 | 0 |
+-------------+----------------+----------------------+----------------+------------+--------+---------+--------+---------+
mysql> SELECT start, end FROM relation_sells_to;
+-------+--------+
| start | end |
+-------+--------+
| 3 | 1 |
| 3 | 2 |
| 2 | 4 |
| 2 | 5 |
| 4 | 2 |
| 1 | 5 |
| 4 | 5 |
| 4 | 6 |
| 4 | 7 |
+-------+--------+
-- Run code to build vcvc table. Code is above.
mysql> SELECT * FROM vcvc WHERE vendor = 3;
+--------+--------+---------+---------+
| vendor | client | vendor2 | client2 |
+--------+--------+---------+---------+
| 3 | 1 | 2 | 4 |
| 3 | 1 | 2 | 5 |
| 3 | 1 | 4 | 2 |
| 3 | 2 | 1 | 5 |
| 3 | 2 | 4 | 5 |
| 3 | 2 | 4 | 6 |
| 3 | 2 | 4 | 7 |
+--------+--------+---------+---------+
Desired Output:
Select client_id, name, city, state, cCount, vCount FROM MAGIC WHERE email = jim#bob.com
+-----------+---------------------+----------------+------------+--------+---------+
| client_id | name | city | state | cCount | vCount |
+-----------+---------------------+----------------+------------+--------+---------+
| 4 | Jon Jon architects | Costa Mesa | California | 67 | 0 |
| 6 | Tims WIndows | Miami | Florida | 10 | 0 |
| 7 | Rons Hot Rods | Costa Mesa | California | 8 | 0 |
| 5 | Joes Pizza | Hamden | Conecticut | 7 | 0 |
+-----------+---------------------+----------------+------------+--------+---------+
-- Ordered by cCount, business_id 2 is NOT in this list because 3 sells to 2 so I don't care that 4 sells to 2.
UPDATE 2:
http://sqlfiddle.com/#!9/fdcec/2
Here is a fiddle of it. The data in the fiddle is slightly different than the data above in that it has 2-3 more connections to properly show what I want.