MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 24th, 2016, 02:03 PM
talesin22 talesin22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2016
Posts: 2 talesin22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 59 sec
Reputation Power: 0
Question Mysql help getting a null data to show up in select query

Here is what i need to achieve:
person_id last_name first_name region_id region name
1 barnum phineas 1 maricopa
2 loman willy 2 pima
2 loman willy 3 pinal
2 loman willy 4 santa cruz
3 kay mary 5 cochise
3 kay mary 6 gila
3 kay mary 7 graham
4 lillian vernon NULL NULL
Here are my tables:
Create database sales
use sales

create table if not exists
`Sales_People`
(`person_id` int primary key,

`last_name` char(16) not null,

`first_name` char(16) not null);

INSERT INTO Sales_people (`person_id`, `last_name`, `first_name`)

values

('1', 'barnum', 'phineas'),

('2', 'loman', 'willy'),

('3', 'kay', 'mary'),

('4', 'lillian', 'vernon');

create table if not exists
`Sales_Region`
(`region_id` int primary key,

`name` char(16) not null);

INSERT INTO Sales_Region (`region_id`, `name`)

Values

('1', 'maricopa'),

('2', 'pima'),

('3', 'pinal'),

('4', 'santa cruz'),

('5', 'cochise'),

('6', 'gila'),

('7', 'graham');

create table if not exists
`Sales_People_Region`
(`person_id` int not null,
`region_id` int not null,

constraint spr_pk primary key(person_id, region_id),

constraint spr_fk1 foreign key(person_id)
references Sales_People(person_id),

constraint spr_fk2 foreign key(region_id)
references Sales_Region(region_id));

INSERT INTO Sales_People_Region (`person_id`, `region_id`)

Values

('1', '1'),

('2', '2'),

('2','3'),

('2', '4'),

('3', '5'),

('3', '6'),

('3','7');

create table if not exists

`Sales` (`year` int not null,

`month` int not null,

`region_id` int not null,

`amount_sold` decimal(11,2),

constraint s_pk primary key(year, month, region_id),

constraint s_fk foreign key(region_id) references
Sales_Region(region_id));

INSERT INTO Sales (`year`, `month`, `region_id`, `amount_sold`)

Values

('2016', '01', '1', '800000'),

('2016', '02', '1', '850000'),

('2016', '03', '1', '990000'),

('2016', '01', '2', '425000'),

('2016', '02', '2', '440000'),

('2016', '03', '2', '450000'),

('2016', '01', '3', '200000'),

('2016', '02', '3', '210000'),

('2016', '03', '3', '220000'),

('2016', '01', '4', '50000'),

('2016','02', '4', '52000'),

('2016', '03', '4', '55000'),

('2016', '01', '5', '40000'),

('2016', '02', '5', '41000'),

('2016', '03', '5', '42000'),

('2016', '01', '6', '3000'),

('2016', '02', '6', '31000'),

('2016','03', '6', '32000'),

('2016', '01', '7', '20000'),

('2016', '02', '7', '21000'),

('2016', '03', '7', '22000');

here is my code:
select sales_people.person_id, `last_name`, `first_name`, sales_region.Region_id,
trim(sales_region.`name`) AS 'Region Name'
from `sales_region`
inner join sales_people_region on sales_people_region.region_id = sales_region.region_id
inner join sales_people on sales_people_region.`person_id` = sales_people.`person_id`
group by sales_region.region_id, sales_people.person_id
having sales_people.person_id >= ''
order by sales_people.person_id, sales_region.region_id asc;

what i get:
person_id last_name first_name Region_id "Region Name"

1 barnum phineas 1 maricopa

2 loman willy 2 pima

2 loman willy 3 pinal

2 loman willy 4 "santa cruz"

3 kay mary 5 cochise

3 kay mary 6 gila

3 kay mary 7 graham

i can't get it to let me see the last person_id because they have no region_ids assigned to them. in the table sales_people_region region_id is a primary key and therefore not able to be null.
if i query sales_people and group by person_id she does come up.

Reply With Quote
  #2  
Old April 24th, 2016, 04:31 PM
talesin22 talesin22 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2016
Posts: 2 talesin22 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 59 sec
Reputation Power: 0
Smile i got this answer from some helpful people and it worked perfectly

select sales_people.person_id, last_name, first_name,
sales_region.Region_id, name AS 'Region Name',
year, sum(`amount_sold`) as 'Y-T-D Sales'
from sales
join sales_people_region
inner join sales_people on sales_people.person_id = sales_people_region.person_id
inner join sales_region on sales_region.region_id = sales_people.person_id
group by sales.region_id, sales_people.person_id
having sales_people.person_id in ('1','2','3')
order by region_id, person_id
;

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Mysql help getting a null data to show up in select query


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap