User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 361,584 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,023 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 580 | Replies: 2
Reply
Join Date: Dec 2007
Posts: 2
Reputation: apbdan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
apbdan apbdan is offline Offline
Newbie Poster

Question nested mysql

  #1  
Dec 20th, 2007
Hi.

I am fairly new to mysql, so help is appreciated.

I have two tables and I wish to count some results from two tables and combine them onto one row, and then join an ID to another table.

The problem is as follows:

Table A:

This lists information of a company_ID and a number of orders placed by that company for a certain item as follows:

hya_ID, company_ID

The following code produces the result I require:

SELECT hya_ID, company_ID, count(*) AS TOTAL from table_name_A GROUP BY company_ID

I then get a result of say:

hya_ID Company_ID total
93 45 2
95 56 4
93 78 8

and so on.

I then have table B:

This has the cost of sales as follows.

hya_ID, size_value, order despatched

I use the following and get the total sales for each item.

SELECT hya_ID, size_value, COUNT(*) AS TOTAL FROM table_name_B GROUP BY size_value WHERE order_despatched_ID=3

I get a result as follows:

hya_ID size_value order_despatched
93 950 1
95 780 0
93 850 1

I then want to show these results, by the common key of hya_ID, in a line, showing the total
number of hya_ID by a company and the total size_value relating to hya_ID in table B and then link to a third table that gives the actual name of the company_ID, and this is table C.

It is:

company_ID, company_name.

So we end up with something like the following.

company_name hya_ID (total) size_value (total)
ABC company 3 340
XYZ company 2 550

and so on and so forth.

Has anyone any ideas how to achieve this.

Thanks.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 172
Reputation: trudge is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: nested mysql

  #2  
Dec 20th, 2007
Sounds like you will need to do a JOIN, but without some more data on your table structures can't specify exactly what. Show us the results of:
describe table_name
for all the tables involved.
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote  
Join Date: Dec 2007
Posts: 2
Reputation: apbdan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
apbdan apbdan is offline Offline
Newbie Poster

Re: nested mysql

  #3  
Dec 21st, 2007
I have now solved it as follows.

SELECT table_A.company_ID AS company, COUNT( * ) AS total, SUM(size_value) AS size, table_B.qbook_ID, table C.company_ID, table_C.company_name						FROM table_A							INNER JOIN table_B ON table_A.hy_ID=table_B.hya_ID				INNER JOIN table_C ON table_B.company_ID = table_C.company_ID
WHERE table_A.despatched_ID=3 AND table_B.nonusd_hy_qbook_ID=1
GROUP BY table_B.company_ID				ORDER BY total DESC 

I do have another question however.

I have table_A that has the countries of a company, these can be countries where several offices are located, they are linked by the company_ID.

So table A contains country 1, country 2, country 3, and so on all linked via the company_ID.

table B contains the details of the main HQ address of the company.

We want to display, all companies, in a row, with their full HQ address from table B, and in the same single row, list all the countries associated with that company in table A.

So you end up with:

company name - company tel - company fax and then a list of country 1, country 2, country 3 in one box without repeating the contry name, tel and fax etc...

Can you help at all?

Thanks.

Arwyn.

Many thanks.

Arwyn.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

All times are GMT -4. The time now is 4:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC