473,498 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query from multiple tables

MySQL 4.14 on FC4

I have a database for a soccer team; simplified:

Roster
- serial (bigint)
- name (varchar)
goals
- serial (bigint)
- gameNumber (bigint) // for linking to a schedule
- playerSerial (bigint) // for linking to Roster
assists
- serial (bigint)
- gameNumber (bigint) // for linking to a schedule
- playerSerial (bigint) // for linking to Roster

I can get a count of goals (or assists) separately, thus:

SELECT COUNT(goals.serial) AS myGoals, Roster.name
FROM goals LEFT JOIN Roster ON goals.playerSerial = Roster.serial
GROUP BY Roster.name
ORDER BY myGoals DESC, Roster.name

How can I get both goals and assists (and, presumably, points = goals +
assists) in the same query?

--
Joe Makowiec
http://makowiec.org/
Email: http://makowiec.org/contact/?Joe
Oct 15 '05 #1
3 1623
You can do this with a UNION join, but it's better design to store both in
the same table and use another column to separate goals and assists, like
this:

serial
gameNumber
goal_assist enum('G', 'A') // G = goal, A=assist
playerSerial

BTW - do you really need a bigint? That seems to me like a waste of storage,
unless you really have to store values exceeding 4 billions (when they're
unsigned).

Markus
Oct 15 '05 #2
On 15 Oct 2005 in mailing.database.mysql, Markus Popp wrote:
You can do this with a UNION join, but it's better design to store
both in the same table and use another column to separate goals and
assists, like this:

serial
gameNumber
goal_assist enum('G', 'A') // G = goal, A=assist
playerSerial
I was wondering about that. Where do I get the count? Otherwise, I'd
add two fields goal/assist (tinyints!), and do a sum rather than a count.
BTW - do you really need a bigint? That seems to me like a waste of
storage, unless you really have to store values exceeding 4 billions
(when they're unsigned).


Don't need it, but I think it was the default. Given the price of
storage, a few bytes here and there shouldn't make that much
difference...

Anyway, thanks for the response.

--
Joe Makowiec
http://makowiec.org/
Email: http://makowiec.org/contact/?Joe
Oct 16 '05 #3
You can count the goals and assists very easily:

select count(*) from table where playerSerial=5 and goal_assist='G';

gives you the goals from player 5,

select count(*) from table where playerSerial=7 and goal_assist='A';

gives you the assists from player 7 or

select count(*) from table where playerSerial=3;

gives you goals and assists from player 3 ;-).

Markus
Oct 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3359
by: Rigga | last post by:
Hi, I am new to mysql and need help on how to join tables. I have a database which contains 4 tables, the main table contains information by date order and the other 3 contain data also in date...
4
4319
by: | last post by:
I have an ACCESS db from a DEXA machine (bone scanner). The data is in seperate tables and I want to link them so that I can query and create one table with all the data I need from the seperate...
2
15026
by: Scott Cannon | last post by:
I am trying to query 3 tables all related by Clinet_ID. The Clients table, Monthly_Expenses table and Monthly_Income table. Each client can have 0>M instances of expenses, past due expenses, and...
2
1354
by: Josh Felker | last post by:
Hey everyone. I have a daily log form with my investing info. In that form I have 2 subforms, the first shows my profits on all the stocks I traded that day, the second shows my profits on...
0
1406
by: Ellen Ricca | last post by:
I have an Access db with several ODBC linked ORACLE tables. These tables have multiple-field PK's. The tables work just fine in many diff types of queries including unmatched queries that are...
1
1996
by: hmiller | last post by:
I'm sorry to populate the server with yet another question about linking multiple tables and queries, howerver I have not been able to find the right criteria. My problem. I am trying to...
5
3051
by: mimo | last post by:
Hello, I have seen samples on how to pull data from one table and save back to it using the Form View control. How do I pull from multiple tables and save back to multiple tables on one...
10
2077
by: Robert | last post by:
I am an attorney in a non-profit organization and a self-taught programmer. I'm trying to create a client db that will allow me to search for potential conflicts of interest based either on Social...
3
30268
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
1
1270
by: crazdandconfusd | last post by:
I have a database with two tables I use for shipping information. One is for if I'm only shipping one item and the other is for a back page of a report for when I ship multiple items. If I'm...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4593
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.