If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help needed with query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-10, 07:22
jacob_144 jacob_144 is offline
Registered User
 
Join Date: Apr 2010
Posts: 5
Question Help needed with query

Hello all, I am in need of help with a query I am trying to make.

For the sake of this example, I have two tables, one called staff, with the following fields:

Quote:
staffid, fname, lname, salary, position
and one called staffSales with these fields

Quote:
staffid, salesfigure, bonus
I would like to make a query which looks into the staffSales table, and finds records which have a salesfigure in excess of 10000.

From here, I would like this to add the bonus field in staffSales, to the salary field in staff, and then return these results.

If anyone could shed some light on whether this would be possible, I would be extremely grateful. Many Thanks
Reply With Quote
  #2 (permalink)  
Old 04-17-10, 08:02
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
you select data from a SQL database using the SELECT statement
you can limit the the data returned by the SELECT statement by using a WHERE clause
you can retrievve the data form more than one table using either a JOIN clause or a WHERE clause, of the two the JOIN is probably the better one to learn, although the WHERE clause can be easier to form for beginners.
SQL can perform all manner of mathmatical and and string operations. it was designed to be legible to English speakers

SELECT my, comma, separated, columns, from MyTable
join AnotherTable on Anotherable.Column=MyTable.column
where acolumn < aNumber

OR

SELECT my, comma, separated, columns from MyTable, AnotherTable
where acolumn < aNumber and Anotherable.Column=MyTable.column

SELECT ((columna * 1.05) + (columnb * 1.175)) as myNewValue from MyTable
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 04-17-10, 09:20
jacob_144 jacob_144 is offline
Registered User
 
Join Date: Apr 2010
Posts: 5
Hi healdem,

That was very useful, thanks for the help. Can I ask one more question? When I run the query:

SELECT ((columna * 1.05) + (columnb * 1.175)) as myNewValue from MyTable

I am returned a really long repeated list of values(84 to be precise!) does this suggest I have something wrong with my tables?

Thanks again
Reply With Quote
  #4 (permalink)  
Old 04-17-10, 10:41
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
so what sql did you use to generate the 84 columns?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 04-17-10, 10:49
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Try:

SELECT a.staffid, a.fname, a.lname, a.salary, a.position, b.staffid, b.salesfigure, b.bonus, (a.salary + b.bonus) AS result
FROM staff a, staffSales b
WHERE a.staffid = b.staffid
AND b.salesfigure > 10000;

Result is the calculation that you are looking for but I have included all the other fields that you might want. If you don't need all the fields just remove and keep the ones you need.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #6 (permalink)  
Old 04-17-10, 11:38
jacob_144 jacob_144 is offline
Registered User
 
Join Date: Apr 2010
Posts: 5
Thats really helpful, thanks guys
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On