Table userdetail
Username Department
user1 dept2
user2 dept3
user3 dept4
Table department
dept1 dept2 dept4 dept3 amount region city
hello bye tc tata 500 pakistan lahore
Now If I select "user1" output should be like that
dept2 dept4 dept3 amount region city
bye tc tata 500 pakistan lahore
Now If I select "user3" output should be like that
dept4 amount region city
tc 500 pakistan lahore
Your design is not realy relational..
Typically a DEPARTNEMT table consist of department id (primary key) and ather attributes.
Also a Query returns always the same number of columns, so it is not possible to return once six columns and four columns for ather value.
Anyway, if the department table has only one row, you may do something like this
select u.Username,
decode(u.Department,'dept2',d.dept2,'dept3',d.dept3, 'dept4',d.dept4) dept,
decode(u.Department,'dept2',d.amount,'dept3',d.amount, 'dept4',d.amount) amount
from userdetail u
cross join department d
where u.Username = 'user1'
USERNAME DEPT AMOUNT
-------- ---- ----------
user1 bye 500
Add other columns based on this schema, if required.
select a.username, b.Department, b.amount, b.region, b.city
from userdetail a inner join
(
select 'dept1' as Department, dept1 as dept, amount, region, city
from department
union all
select 'dept2' as Department2,dept2 as dept2, amount, region, city
from department
union all
select 'dept3' as Department3,dept3 as dept3, amount, region, city
from department
union all
select 'dept4' as Department4,dept4 as dept4, amount, region, city
from department
) b on (a.department=b.Department)
where a.username= 'user1';
Related
I have two tables like this:
Employee Table:
EmployeeID
firstName
lastName
1
Johnny
Depp
2
Rebecca
Smith
3
Rodger
Doe
Sales Table:
EmployeeID
Sales
1
100.20
2
200.19
3
355.23
And I'd like to join the tables to do something like this:
EmployeeID
fullName
Sales
1
Johnny Depp
100.20
2
Rebecca Smith
200.19
3
Rodger Doe
355.23
How would I do that? Here's what I tried so far:
SELECT employee.firstName + employee.lastName AS fullName, employeeID, sales
FROM employee i
INNER JOIN Sales s ON s.customerID = i.CustomerID
I'm getting a syntax error at my "+" symbol.
What's my problem?
as #Gordon said ,use CONCAT():
SELECT CONCAT(employee.firstName, ' ', employee.lastName) AS fullName
, employeeID
, sales
FROM employee i
INNER JOIN Sales s
ON s.customerID = i.CustomerID
Below is the table structure:
I have three tables : employee, skill and employee_skills.
Employee: id, firstname, lastname, etc.....
Skill : id, title, description
Emplyoee_skills : id, employee_id (FK of employee table), skill_id(FK of skill table)
Now, I want the below output:
Employee
Id firstname lastname
1 Rajnikant Patel
2 Steve Jobs
3 Sachin Tendulkar
4 Ratan Tata
Skill
Id title description
1 java java
2 mongodb mongodb
3 PHP PHP
4 spring Spring framework
Employee_skills
Id employee_id skill_id
1 1 1
2 1 2
3 2 1
4 3 2
So I want the query which can return the employee records who have the skills that are passed:
Let's say, I pass in where clause : s.title in ('mongodb', 'java'), then it should return record:
Id firstName lastName
1 Rajnikant Patel
Because this employee has both the skills.
Here is how to do it: Select all employee skills for the desired skills, then only keep employees having the full count.
select *
from employee
where id in
(
select employee_id
from employee_skills
where skill_id in
(
select id
from skill
where title in ('java', 'mongodb')
)
group by employee_id
having count(distinct skill_id) = 2
);
When adding another skill, you must check for a count of 3, of course, etc.
Below query will give you desired results.
SELECT e.id, e.firstname, e.lastname
FROM Employee e
JOIN Employee_skills es
ON es.employee_id = e.id
JOIN Skill s
ON es.skill_id = s.id
WHERE s.title IN ('mongodb', 'java')
GROUP BY e.id
HAVING count(s.id) = 2
If I have two tables:
school:
school_id | class_id | school_location
-------------------------------------------
400 50 Arizona
staff:
staff_id | forename | school_id | wage
------------------------------------------
1 Peter 400 5000
How could I get the output the number of staff that work in school and the salary of each school.
For example:
school_id | numberofstaff | salary
---------------------------------------
400 | 1 | 5000
I know I should join the tables like so:
SELECT school_id
FROM school
INNER JOIN staff
ON school.school_id = staff.school_id
However I am unsure how to do the remaining part of the query.
SELECT numberofstaff COUNT(numberofstaff) from staff
GROUP BY school_id
Union all
select 'SUM' numberofstaff, COUNT(numberofstaff)
FROM staff
This should give me my school ids with the number of staff belonging to each school_id.
Once you join both tables, you can then count the staff and sum all the wages for each school:
SELECT sc.school_id,
COUNT(*) as numberofstaff,
SUM(st.wage) as salary
FROM school sc
INNER JOIN staff st
ON sc.school_id = st.school_id
GROUP BY sc.school_id
try this way
SELECT COUNT(staff.numberofstaff) as numberofstaff , school.school_id as school_id FROM school
INNER JOIN staff
ON school.school_id = staff.school_id
GROUP BY school.school_id;
I am looking for a way to list all rows with a duplicate column value.
Example:
Table Address
House Person
23 Joe
23 Jane
27 Chris
29 Grandpa
Expected output:
House Person
23 Joe
23 Jane
I would like to do this so I can manipulate values of people who live in the same house.
I would start by getting all house ids that have more than one person:
SELECT house
FROM myTable
GROUP BY house
HAVING COUNT(*) > 1;
Then, you can join that with your original table to get the people who live in those houses:
SELECT m.*
FROM myTable m
JOIN (SELECT house FROM myTable GROUP BY house HAVING COUNT(*) > 1) temp
ON temp.house = m.house;
Here is an SQL Fiddle example.
Try this :
select b.house, a.person from
(
select person,house
from address
)a,
(
select house,count(house) as cnt
from address
group by house
)b
where a.house = b.house
and b.cnt > 1
In mp_cities table city_name and city_id are the fields
city_id city
--------------------
1 Chennai
2 Bangalore
3 Kerala
In profile table
user_email city_type
------------------------------
abc#gmail.com 1,2,3
I am using the following query
SELECT city_name
FROM mp_cities
WHERE city_id IN (SELECT city_type
FROM profile
WHERE user_email='abc#gmail.com')
this query will result Chennai.
I have to get all the cities
You can use FIND_IN_SET() in MySQL.
SELECT a.*
FROM mp_cities a
INNER JOIN profile b
ON FIND_IN_SET(a.city_id, b.city_type)
WHERE b.user_email = 'abc#gmail.com'
but the best way so far I can think is to normalize the table properly.
mp_cities
city_id (PK)
city
other columns
profile
user_id (PK)
other columns
mp_cities _profile
city_id (FK)
user_id (FK)
You would get the desired result if the profile table looked like this:
user_email city_type
------------------------
abc#gmail.com 1
abc#gmail.com 2
abc#gmail.com 3
That is, the inner SELECT must return several rows, each with a number, NOT one row with a comma-delimited set of numbers.
Try to join both tables with FIND_IN_SET() function like this:
SELECT c.*
FROM mp_cities c
JOIN profile p
ON FIND_IN_SET(c.city_id,p.city_type) > 0;
See this SQLFiddle