📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!

SQL queries - Help needed please

tincat
tincat Posts: 935 Forumite
edited 2 February 2011 at 11:03AM in Techie Stuff
Hi,

I have a number of tables and have to perform queries on them. Some are easy but some are a bit complicated.

Can anyone help me with the following please?

Picture 3 tables:

employee: employeeNo, branchNo, positionId, firstName, lastName, Address etc
branch: branchNo, branchAddress, branchTelephone
position: positionId, positionName

1) I need to display the manager name, branch number and branch telephone number. The 'manager' position has a positionId = 1.

I tried a number of things eg:

SELECT employee.FirstName employee.LastName branch.branchNo branch.branchAddress WHERE position.positionId = 1

but haven't been able to get it to work.

2) The employee record has a field for birth dates for the employees. I need to list all employees over the age of 25. Now I know the SQL for that : SELECT COUNT (column) FROM table WHERE age > 25, but I don't know how to translate the birthdate into an age and I can't find a formula that fits a birthdate.


Please help to explain if possible. Many thanks.
«13

Comments

  • tincat
    tincat Posts: 935 Forumite
    SELECT employeeFirstName, employeeLastName from employee
    INNER JOIN branch
    ON employee.branchId=branch.branchId
    WHERE employee.positionId=1

    The above query returns the names of the managers, but when I try to add in the branch details (address, phone number and id) it all goes wrong.

    If you can explain would be so grateful
  • victor2
    victor2 Posts: 8,675
    Part of the Furniture 1,000 Posts Name Dropper
    Ambassador
    edited 2 February 2011 at 10:45AM
    What links the tables? You only show positionId in the employee table. The employee and branch tables presumably have branchId as a common field. Is positionId also in the position table?
    Also, are you using Microsoft SQL, or another variant (although they are very similar)?

    Edit:
    Look at the DATEDIFF function to calculate age - that does vary with different SQL database engines.

    I’m a Forum Ambassador and I support the Forum Team on the In My Home MoneySaving, Energy and Techie Stuff boards. If you need any help on these boards, do let me know. Please note that Ambassadors are not moderators. Any posts you spot in breach of the Forum Rules should be reported via the report button, or by emailing forumteam@moneysavingexpert.com. 

    All views are my own and not the official line of MoneySavingExpert.

  • are the dates written in as human dates or

    sql dates i think sql dates are written a bit strange

    it could be usefull if you could generate a colunm with there age in maybe calculate it from the date of birth and the current date
  • Can you please post all the fields for the database tables.

    You should be able to use "DATEDIFF" to calculate the difference between the birthday and the current date, and then calculate the number of years from that.
  • victor2
    victor2 Posts: 8,675
    Part of the Furniture 1,000 Posts Name Dropper
    Ambassador
    it could be usefull if you could generate a colunm with there age in maybe calculate it from the date of birth and the current date

    You don't really want to store their age - it has a habit of changing as time goes by! :)
    Better to calculate it from date of birth when required, unless you want to store their age as of a particular event.

    I’m a Forum Ambassador and I support the Forum Team on the In My Home MoneySaving, Energy and Techie Stuff boards. If you need any help on these boards, do let me know. Please note that Ambassadors are not moderators. Any posts you spot in breach of the Forum Rules should be reported via the report button, or by emailing forumteam@moneysavingexpert.com. 

    All views are my own and not the official line of MoneySavingExpert.

  • tincat
    tincat Posts: 935 Forumite
    victor2 wrote: »
    What links the tables? You only show positionId in the employee table. The employee and branch tables presumably have branchId as a common field. Is positionId also in the position table?
    Also, are you using Microsoft SQL, or another variant (although they are very similar)?

    Edit:
    Look at the DATEDIFF function to calculate age - that does vary with different SQL database engines.

    I'm using phpMyAdmin.

    Yes, positionId is in the employee table and the position table. Sorry. I will go and edit that post. I'll google the DATEDIFF function, thanks
  • tincat
    tincat Posts: 935 Forumite
    are the dates written in as human dates or

    sql dates i think sql dates are written a bit strange

    it could be usefull if you could generate a colunm with there age in maybe calculate it from the date of birth and the current date

    They're entered in as 1984-03-25
  • victor2
    victor2 Posts: 8,675
    Part of the Furniture 1,000 Posts Name Dropper
    Ambassador
    Are you using a MySQL database then? phpMyAdmin often goes along with that.

    I’m a Forum Ambassador and I support the Forum Team on the In My Home MoneySaving, Energy and Techie Stuff boards. If you need any help on these boards, do let me know. Please note that Ambassadors are not moderators. Any posts you spot in breach of the Forum Rules should be reported via the report button, or by emailing forumteam@moneysavingexpert.com. 

    All views are my own and not the official line of MoneySavingExpert.

  • tincat
    tincat Posts: 935 Forumite
    Can you please post all the fields for the database tables.

    You should be able to use "DATEDIFF" to calculate the difference between the birthday and the current date, and then calculate the number of years from that.

    Sorry, my initial post wasn't clear

    employee: employeeNo, branchNo, positionId, firstName, lastName, Address, gender, employeeSalary
    branch: branchNo, branchStreetAddress, branchTown, branchPostcode, branchTelephone
    position: positionId, positionName, positionDescription

    The foreign keys in blue.

    Thanks
  • tincat
    tincat Posts: 935 Forumite
    victor2 wrote: »
    Are you using a MySQL database then? phpMyAdmin often goes along with that.

    I think so. Sorry, I'm new to this.

    The database tables are all entered into phpmyadmin, and I've set primary keys and foreign keys among all the tables (there are 10 tables in all), and then I have to do the SQL queries from phpMyAdmin.

    all the tables are in innoDB if that makes any difference. I'm just going to google the datediff function, but will be back as soon as poss for any help.

    Thanks all
This discussion has been closed.
★ ★ ★ Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 355.9K Banking & Borrowing
  • 255K Reduce Debt & Boost Income
  • 456.2K Spending & Discounts
  • 248.4K Work, Benefits & Business
  • 606.1K Mortgages, Homes & Bills
  • 179K Life & Family
  • 263.9K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16.2K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.