📨 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

2

Comments

  • SELECT firstName, lastName, employee.branchNo, branchTelephone
    FROM employee, branch
    WHERE positionId == 1 AND employee.branchNo == branch.branchNo

    This should work for the first one.
  • I haven't done it for a while but try this
    SELECT employeeFirstName, employeeLastName, whatever you want from branch table eg branch.location from employee, branch
    INNER JOIN branch
    ON employee.branchId=branch.branchId
    WHERE employee.positionId=1
    or have you tried that already??
  • victor2
    victor2 Posts: 8,675
    Part of the Furniture 1,000 Posts Name Dropper
    Ambassador
    ladymarmalade1970's query should work.
    Another way is:
    SELECT * FROM employee as t1 inner join branch as t2 on t1.branchNo=t2.branchNo where t1.positionId=1

    For the age, you could include the following in the field list:
    round(datediff(curdate(),dob)/365.25) as age

    where dob is a date field in the employee table containing the date of birth, which will show as yyyy-mm-dd when you browse it.
    The age calculation is not perfect and could be thrown out a year around birthdays. There is a way round it, but it gets quite complicated!

    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
    edited 2 February 2011 at 1:55PM
    SELECT FirstName, Lastname, DateOfBirth, CURDATE(),
    DATEDIFF(year,'DateOfBirth', year,'CURDATE()') AS age
    FROM employee
    Where age >= 25 AND jobId = 1

    This is what I came up with for the datediff one, but my error message is

    #1582 - Incorrect parameter count in the call to native function 'DATEDIFF'.

    This following option did actually list all employees and showed their age in years, but I couldn't get it to only show the over 25's.
    SELECT FirstName, Lastname, DateOfBirth, CURDATE(),
    (YEAR( CURDATE( ) ) - YEAR( dateOfBirth) ) - (RIGHT( CURDATE( ), 5) < RIGHT( dateOfBirth, 5) )
    AS age FROM staff
    But if I add
    WHERE age >= 25
    it comes up with the error: '#1054 - Unknown column 'age' in 'where clause''



    Regarding the first question, thank you so much scottishblondie. It worked although it didn't work with the ==. I had to use just one =.

    I tried that method first, so thanks ladymarmalade and victor for your suggestions. I would have tried them next.
  • victor2
    victor2 Posts: 8,675
    Part of the Furniture 1,000 Posts Name Dropper
    Ambassador
    Unfortunately you have to put the entire expression in again instead of age in the where statement.

    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
    Hi again,

    3) I need to list all the branches and give the total monthly salary paid out.
    SELECT branchNo, sum(AnnualSalary)/12 as MonthlySalary from employee where branchNo = 5

    This statement works and gives me a 2 column table with branchNo and Monthly Salary (average) but only for one branch.

    Does anyone know how to add to the statement to list all of the branch numbers?

    Thanks
  • tincat
    tincat Posts: 935 Forumite
    victor2 wrote: »
    Unfortunately you have to put the entire expression in again instead of age in the where statement.

    I'm not sure how to do that. Please could you explain? Do I have to use () or ?
  • tincat
    tincat Posts: 935 Forumite
    victor2 wrote: »
    Unfortunately you have to
    put the entire expression in again instead of age in the where statement.

    OMG it worked - you are such a star. All of you are.

    If you have a last idea of the average salary question that would be a hattrick. :j
  • victor2
    victor2 Posts: 8,675
    Part of the Furniture 1,000 Posts Name Dropper
    Ambassador
    tincat wrote: »
    OMG it worked - you are such a star. All of you are.

    If you have a last idea of the average salary question that would be a hattrick. :j

    Try something like:
    SELECT branchNo,sum(AnnualSalary)/12 as MonthlySalary from employee group by branchNo

    The group by is the key to that one!

    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.

  • buglawton
    buglawton Posts: 9,246
    Part of the Furniture 1,000 Posts Name Dropper
    Forumite
    If you are using PHPMyAdmin, it implies the data is on a host accessible to the internet. With such sensitive employee data, better make darn sure it's secure. Wiser to run the DB on your own PC (if you are not doing so already).
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.