I have two tables: Employees and Pharmacies.
In table Employees I have a column name Pharmacy which tells me what pharmacy an employee works at, and also a column with the names of the employees.
In table Pharmacies I have a column of pharmacies and I want to add a new one named Number_of_employees which to contain the number employees from the corresponding pharmacy where he works.
Any ideas how to do this ?
DON'T ADD the column Number_of_employees
you can return Number_of_employees value by query
Select Pharmacies.*, count(Employees.Pharmacy) as Number_of_employees from Pharmacies
join Employees on Pharmacies.id = Employees.Pharmacy
group by Employees.Pharmacy
Related
The database name is employee-information in that database I was trying to show one "person_name" column that is available in both tables "works" & "employee" by using two different conditions that will filter values in each table.
So that I can see the values filtered by the two conditions from both tables in one single "person_name" column.
work & employee tables,
Here is what I have done so far,
USE employee_information;
SELECT employee.person_name, works.person_name
FROM employee, works
WHERE city = "Miami" AND salary > 50000;
The result I am getting,
For that command I am getting this two-column from both table. Conditions are working but values are repetitive and there are two columns but I need to show one column filled with the value from both tables where those two conditions are true
My desired result is,
person_name//table name
Alex //values those are true by both condition in each table
Robin Hood
You need to join the tables using person_name as the relationship.
SELECT employee.person_name
FROM employee
JOIN works ON employee.person_name = works.person_name
WHERE employee.city = 'Miami' AND works.salary < 50000;
In your case you can use JOIN , here is example
SELECT w.*,e.company_name,e.salary FROM works w INNER JOIN employee e ON e.person_name = w.person_name WHERE city = "Miami" AND salary > 50000;
you must add a primary key in "employee table" with name id
and a foreign key in "work table" with name employee_id
and your query will be
SELECT employee.person_name
FROM employee
WHERE employee.id, works.employee_id
and city = "Miami" AND salary > 50000;
I have one Table of users in which their is LOGIN_ID, LOGIN_PASSWORD, and USER_ROLE.
I want to get Name of USERS From Different Table with single query.
e.g.
Login_ID: 1001 is from students so we have to get the name form students.
Login_ID: 235738932 is from Employees so we have to get name from Employees.
There is no Foreign Key Relation in users Tables because Login_id is from two different Tables.
Try:
SELECT
LOGIN_ID,
LOGIN_PASSWORD,
USER_ROLE,
CONCAT(COALESCE(students.name,''),COALESCE(employee.name,'')) as Name
FROM users
LEFT JOIN students ON students.Login_ID = users.Login_ID
LEFT JOIN employee ON employee.Login_ID = users.Login_ID
COALESCE(students.name,'') Will return the students name. If the name is not found, the values will be NULL, and COALESCE will change this to ''.
The same happend to the name of the employee. Both names are getting concatenated.
I have a Transaction table (named G7) that has fields of
1. ID
2. Module ID
3. Employer ID
4. Employee ID (From) - **refers to Employee ID Table** -
5. Employee ID (To) - **refers to Employee ID Table** -
6. Transaction Date
And the Employee ID (named I9) table that has fields of
1. Employee ID
2. Employee First Name
3. Employee Last Name
4. Employee Email Address
In the output SQL statement report, I need to have the transaction report (based on (G7)
1. Transaction Date
2. Employee First Name (From)
3. Employee Last Name (From)
4. Employee First Name (To)
5. Employee Last Name (To)
It's annoying that I still can't get the SQL statement to work. Can someone please help me?
This should do it:
SELECT Transaction.TransactionDate,
A.FirstName,
A.LastName,
B.FirstName,
B.LastName
FROM Transaction
JOIN Employee AS A ON Transaction.From = A.EmployeeID
JOIN Employee AS B ON Transaction.To = B.EmployeeID
If you try to get data with a different meaning of the same table, make sure to create a difference between them by defining an alias with "AS". Besides you have to define the join in the proper way. I guess to find the combination of both was your problem.
I have two tables States and Cities.
In teststates table: there are data regarding states of different countries
The code column has id for states associated with it
In Cities table:
there is region column which has same id as of code column in teststates table.
Requirement:
I want the Code in mysql to fetch id from the teststates table column and replace it into region column in testcities table as I want only two columns which are city name and region in testcities table. Please help me!
select a.name,b.region from teststates a,testcities b where a.country = b.country
This will return City Name and Region Name columns joining on Country name from both tables.
I have one database that contains two tables named 'players' and 'morestats'
In the table 'players' I have the columns 'id' and 'player_name'
In the table 'morestats' I have the columns 'id' and 'stat1', 'stat2', etc..
I have to get the value 'stat1' from the table 'morestats' and then search for the player_name of the table 'players' that is egual to the id of both the tables.
All I have to do is this:
Search for the row that has the same ID from 'morestats' and 'players', then get player name.
SELECT stat1, player_name
FROM morestats
INNER JOIN players
USING(id);
This will display a table with each row containing the value of stat1 and the corresponding player_name.
If you want to filter this table you have to put an WHERE statement before the end of the query. Example: WHERE stat1 = 'valuetosearch';