Ready, steady… save!
You don’t need a chart to join in. Here are 2 ways to record your savings:
1. Cross it off
Pop a chart into your first post. Saved an amount? Cross it out! Read more »
£0.01 £0.02 £0.03
2. Write it down
Skip the chart and add updates to your first post. Read more »
“Another +£3 saved today. Go me!”
Use +£ before each new saving so it counts towards your total.
📨 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
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.
Comments
-
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 grateful0 -
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.
0 -
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 date0 -
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.0 -
hubert_cumberdale wrote: »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.
0 -
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, thanks0 -
hubert_cumberdale wrote: »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-250 -
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.
0 -
scottishblondie wrote: »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.
Thanks0 -
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 all0
Confirm your email address to Create Threads and Reply
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