I dont't now how to explain but i have two table in my database.
tab 1: service
tab 2: department
service table:
idService
serviceName
department_ID
is_deleted
department table:
departmentId
departmenetName
is_deleted
I want select all services with the departement name but with departement also if there is no service assigned to it.
if there is no service assigned to a departement then the flelds will be shown but i must have null or equivalent in idService field.
The result would be like this :
serviceID
serviceName
departmentId
departmenetName
1
IT
2
SI
2
Maintenance
6
Mechanical
3
Maintenance
6
Mechanical
4
Opt Manager
7
Finance
5
Instrument
5
Electric
NULL
NULL
1
Civil
8
Agro
NULL
NULL
_________
___________
____________
_______________
I have tried all kind of join options but I couldn't find the logic behind.
I use Mysql as DB
Any idea please ? Thank you.
you could try using an union between the left joined tables whit and without service
select a.serviceID, a.serviceNam, b.departmentId, b.departmenetName
from service a
left join department b on a.department_ID = b.departmentId
UNION
select null, null, b.departmentId, b.departmenetName
from department b
left join service a a.department_ID = b.departmentId
where a.serviceID is null
Related
I have three tables, clients, job_allocations and jobs table. I want to select all clients that are not in a particular job, below are my tables.
Clients table
id
Fullname
1
John Doe
2
Jane Doe
3
King James
4
Jere Gray
Jobs table
id
Title
1
Road Construction
2
Repair of Engines
job_allocations table
id
client_id
job_id
1
2
1
2
2
2
3
1
2
4
3
2
I want to select all clients that are not in job_id=2, but when I ran my query, I am getting client id: 2 - Jane Doe again, please how do I solve this?
I did this:
LEFT JOIN job_allocations ON job_allocations.client_id = clients.id
WHERE job_id <> 2 OR job_id IS NULL```
You can use a NOT IN clause as follows:
SELECT *
FROM clients
WHERE id NOT IN (SELECT client_id
FROM job_allocations
WHERE job_id = 2)
Check the demo here.
So you will fetch all clients, but only jobs related to job_id <> 2
This query should work for you:
SELECT client.*
FROM clients
LEFT JOIN job_allocations ON job_allocations.client_id = clients.id and job_id <> 2
Use DISTINCT keyword for selecting unique values
I want to get the output of 2 tables by removing the NULL
Emp Table
id name dept
1 Null EE
2 Ravi Null
NULL Mani CSE
Stud Table
id name dept
1 Manju NULL
2 NULL ECE
3 Mani CSE
Output
id name dept
1 Manju EE
2 Ravi ECE
3 Mani CSE
You can try below - using left join and coalesce() function
select a.id, coalesce(a.name,b.name) as name, coalesce(a.dept,b.dept)
from stud a left join emp b
on a.id=b.id
I don't know why your ID is nullable, from what I can see it is the primary key of you table. But if you want to select all data removing the NULL. Just add a condition.
For example:
SELECT COALESCE(e.name, s.name) name,
COALESCE(e.dept, s.dept) dept
FROM emp e
LEFT JOIN stud s
ON s.id = e.id
But honestly, you should check the way you created your database. As someone commented, this is not a relational table.
I am trying to join multiple columns on a MySQL table. I can't figure out how I can retrieve multiple columns into one single row, so I can display one row on my page.
table: employee_timesheet
id employee_id service_area_name sign_off_1 sign_off_2 sign_off_3 created_date status last_updated
1 2 London Rom Director NULL 2015-02-11 17:22:44 Submitted 2015-02-11 17:22:44
table (empty): employees_timesheet_sign_off_team_leaders
id employee_id timesheet_id sign_off_key team_leader_id
table: employees_timesheet_sign_off_roms
id employee_id timesheet_id rom_id sign_off_key
1 2 1 4 sign_off_1
2 2 1 5 sign_off_1
table: team_leaders
id first_name last_name email reports_to status date_added last_updated
2 Bob Test me#mydomain.com 4 Active 2015-02-03 12:50:25 2015-02-03 13:28:56
table: roms
id first_name last_name email status date_added last_updated
4 Bill Gates bill#mydomain123445678.com Active 2015-02-03 13:14:07 2015-02-03 13:28:40
5 Ben Morris ben#mydomain123445678.co.uk Active 2015-02-11 17:35:43 NULL
I need to join the above tables to get the following result:
ID: 1
Team Leader: (null)
Rom: Bill Gates,Ben Morris
Date: 2015-02-11 17:22:44
It would be very much appreciated if someone could help with this.
Thanks in advance.
For your use case, you need to use the GROUP_CONCAT function: http://www.percona.com/blog/2013/10/22/the-power-of-mysqls-group_concat/. The query would look something like:
SELECT et.id, CONCAT(l.first_name, ' ', l.last_name) 'team_leader', GROUP_CONCAT(DISTINCT CONCAT(r.first_name, ' ', r.last_name)), et.last_updated
FROM employee_timesheet et
INNER JOIN employees_timesheet_sign_off_roms etr ON et.id = etr.timesheet_id
INNER JOIN roms r ON etr.rom_id = r.id
LEFT JOIN employees_timesheet_sign_off_team_leaders etl ON et.id = etl.timesheet_id
LEFT JOIN team_leaders l ON etl.team_leader_id = l.id
GROUP BY et.id, team_leader, et.last_updated
I have three tables called 'Employees', 'Managers', 'Supervisors'
Employees
ID Name
1 Bob
2 Alex
3 Joe
4 Melissa
5 Hannah
Managers
MID Name
1 Bob
3 Joe
Supervisor
SID Name
3 Joe
4 Melissa
I am trying to check whether if an employee is either a manager or a supervisor or both, and have a result table that looks like
ID Manager Supervisor
1 Manager NULL
2 NULL NULL
3 Manager Supervisor
4 NULL SUPERVISOR
5 NULL NULL
I want to try using a case statement in order to determine whether a fellow Employee ID is either a manager and/or a supervisor. But i'm not sure how to do this.
I know that I have to do something like
SELECT e.id /*how do i select the last 2 columns bc they are new?*/
FROM Employees e, Managers m, Supervisor s
CASE WHEN e.id = m.mid THEN 'manager' /*i don't think this is the correct format*/
CASE WHEN e.id = s.sid THEN 'supervisor'
....
You want to use left join. Follow a simple rule: do not use commas in the from clause. Always use explicit join syntax. Ignore anyone or anything that tells you otherwise. Such advice is very out-of-date.
select e.id,
(case when m.mid is not null then 'Manager' end) as Manager,
(case when s.sid is not null then 'Supervisor' end) as Supervisor
from employees e left join
managers m
on e.id = m.mid left join
supervisors s
on e.id = s.sid;
Also note that your tables are not properly normalized. Because managers and supervisors are employees, you should only have the name column in the employees table. It should not be repeated in the other tables, unless the same person could have different names when they take on the different roles.
Try this:
SELECT ID , (
SELECT MID AS Manager
FROM Managers WHERE MID = ID
LIMIT 1
) AS Manager, (
SELECT SID AS Supervisor
FROM Supervisors WHERE SID = ID
LIMIT 1
) AS Supervisors
FROM Employees
This will display your result as follows
ID Manager Supervisor
1 1 NULL
2 NULL NULL
3 3 3
4 NULL 4
5 NULL NULL
I just started learning Sql, so go easy on me. I'm trying to list out the item description for each toy, along with the customer names who bought them, as well as the dates of purchase.
Using aliases, I was able to get the name and the date_of_purchase using the following statement:
select tp.date_of_purchase, tp.name from table_a AS tp
left outer join table_b AS tt on tp.id = tt.id;
.. But I can't figure out how to get the item description from table_a as well. I need to get the name and date_of_purchase from tablea and the item_description from table_b using one sql statement. I've tried everything, but can't get it to work.
Here are the tables below:
table_a:
Column 1: id int(4) unsigned
Column 2: name varchar(100)
Column 3: quantity int(4) unsigned
Column 4: date_of_purchase date
50 John Miller 3 2013-03-16
25 Brad Cooper 1 2013-11-01
50 Alison Bradey 3 2013-11-01
50 Melissa Patterson 2 2014-02-25
75 Lisa Simpson 10 2015-08-15
table_b:
Column 1: id int(4) unsigned
Column 2: item_description varchar(100)
Column 3: quantity int(4) unsigned
Column 4: price decimal(5,2)
15 Super Nintendo 3 100.00
25 Action Figure 1 15.00
50 Gameboy 3 65.00
20 Nintendo 64 2 150.00
Any help would really be appreciated.
item_description is located in tableb(aka tt), so all you need to do is add that field (with a tt prefix since it's in tableb) to the select list;
SELECT tp.date_of_purchase, tp.name, tt.item_description
FROM table_a AS tp
LEFT OUTER JOIN tableb AS tt
ON tp.id = tt.id;
An SQLfiddle to test with.