•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 427,218 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,208 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: Programming Forums
Views: 756 | Replies: 2
![]() |
| |
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 176
Reputation:
Rep Power: 2
Solved Threads: 20
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:
for all the tables involved.
describe table_name
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
"Others make web sites. We make web sites work!"
•
•
Join Date: Dec 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
I have now solved it as follows.
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb MySQL Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Clean Previous Next Script for MySQL results (PHP)
- Python Tuples and Mysql Query (MySQL)
- Please help me in connecting JSP to MySql (JSP)
- MySql connection question. (Java)
- need advice on nested loops. (PHP)
- connecting jsp with mysql database (JSP)
- MySQL nested query / joined query conversion help (MySQL)
- MySQL vs. PostgreSQL (MySQL)
Other Threads in the MySQL Forum
- Previous Thread: code to transfer data from mysql to excel sheet
- Next Thread: MySQL return month and date from NOW()


Hybrid Mode