I have a table called tbl_appointment which has 2 foreign keys: idclient and idemployee which they are referencing tbl_persons.
So what i want to do is instead of getting the id of idclient and idemployee, i want to get the names (name,last_name,last_sname) which they belong to those id.
Query
SELECT CONCAT(tbl_persons.name,' ',tbl_persons.last_name,' ',tbl_persons.last_sname) as fullname, tbl_appointment.*
FROM tbl_appointment
INNER JOIN tbl_persons ON tbl_persons.idpersons = tbl_appointment.idemployee
INNER JOIN tbl_persons ON tbl_persons.idpersons = tbl_appointment.idclient
WHERE idclient= '$user';
You can use the following query, using a LEFT JOIN instead of INNER JOIN:
SELECT
CONCAT(p1.name, ' ', p1.last_name, ' ', p1.last_sname) AS client_fullname,
CONCAT(p2.name, ' ', p2.last_name, ' ', p2.last_sname) AS employee_fullname
FROM tbl_appointment ta
LEFT JOIN tbl_persons p1 ON ta.idclient = p1.idpersons
LEFT JOIN tbl_persons p2 ON ta.idemployee = p2.idpersons
WHERE ta.idclient = '$user'
Try the following
SELECT
app.*,
CONCAT(client.name,' ',client.last_name,' ',client.last_sname) AS client_full_name,
CONCAT(empl.name,' ',empl.last_name,' ',empl.last_sname) AS empl_full_name
FROM tbl_appointment app
LEFT JOIN tbl_persons client ON app.idclient=client.id
LEFT JOIN tbl_persons empl ON app.idemployee=empl.id
WHERE app.idclient= '$user'
Try the following code: Only thing is you have to select the clients and employees separately, together is more difficult
SELECT name,last_name,last_sname
FROM tbl_persons WHERE id IN (
SELECT DISTINCT idclient FROM tbl_appointment;
); -- This will select all the clients
SELECT name,last_name,last_sname
FROM tbl_persons WHERE id IN (
SELECT DISTINCT idemployee FROM tbl_appointment;
); -- This will select all the employees
Related
I have the following query:
SELECT
STRING_AGG(SELECT department FROM additionaldepartments WHERE employee = e.EMPID, ',') AS additional_departments,
r.actionId,
e.EMPID AS `id`,
CONCAT(e.FIRSTNAME, ' ', e.LASTNAME) AS `name`,
b.ID AS `branch_id`,
b.NAME AS `branch_name`,
b.EMAIL AS `branch_email`,
d.departmentDescription AS `primary_department`,
j.JobType AS `job_role`
FROM
actionplanresponsibleemployees r
JOIN
employee e ON e.EMPID = r.employeeId
JOIN
branch b ON b.ID = e.BRANCHID
LEFT JOIN
hremploymentdepartments d ON d.departmentPK = e.DepartmentFK
JOIN
hrjobtype j ON j.JobTypeID = e.JOBTYPE
The first line of the select will not work, I know this, but I'm just trying to demonstrate what it is I want from this query,
What I'm trying to achieve, is to have another column in my select called additional_departments which would be the department field from my additionaldepartments table, joined together in a string separated with commas.
What you want is GROUP_CONCAT():
SELECT
(SELECT GROUP_CONCAT(department)
FROM additionaldepartments
WHERE employee = e.EMPID) AS additional_departments,
r.actionId,
........................
I have 3 tables in my db
1)cushbu_users (id,first_name,last_name)
2)cushbu_art (id,user_id(FK cushbu_user),title,base_price etc...) -for store user arts
3)cushbu_mark_user_favorites (id,user_id(FK cushbu_user),art_id(FK cushbu_art)) -for marking favourite items
I want to fetch the all art items of a particular user favourited
with count of favourites each art (stored in cushbu_mark_usier_favorites table )
Here is my query
SELECT
cushbu_art.id AS art_id,
cushbu_art.title,
cushbu_art.base_price,
cushbu_art.image_name,
CONCAT(
cushbu_users.first_name,
' ',
cushbu_users.last_name
) AS artist_name,COUNT(cushbu_mark_user_favorites.art_id)
FROM
cushbu_art
JOIN cushbu_mark_user_favorites ON cushbu_mark_user_favorites.art_id = cushbu_art.id
JOIN cushbu_users ON cushbu_users.id = cushbu_art.artist_id
LEFT JOIN cushbu_mark_user_favorites ON cushbu_art.id=cushbu_mark_user_favorites.art_id
WHERE
cushbu_mark_user_favorites.user_id = 68
But i got Not unique table/alias: 'cushbu_mark_user_favorites' this join statement
LEFT JOIN cushbu_mark_user_favorites ON cushbu_art.id=cushbu_mark_user_favorites.art_id
UPDATE
SELECT
a.id AS art_id,
a.title,
a.base_price,
a.image_name,
CONCAT(
c.first_name,
' ',
c.last_name
) AS artist_name,COUNT(b.art_id)
FROM
cushbu_art a
JOIN cushbu_mark_user_favorites b ON b.art_id = a.id
JOIN cushbu_users c ON c.id = a.artist_id
LEFT JOIN b ON a.id=b.art_id
WHERE
b.user_id = 68
Try below query.
SELECT
cushbu_art.id AS art_id,
cushbu_art.title,
cushbu_art.image_name,
CONCAT(
cushbu_users.first_name,
' ',
cushbu_users.last_name
) AS artist_name , b.favorites_count as total_fav
FROM
cushbu_mark_user_favorites
LEFT JOIN cushbu_art ON cushbu_art.id=cushbu_mark_user_favorites.art_id
LEFT JOIN cushbu_users ON cushbu_users.id = cushbu_art.artist_id
LEFT JOIN (SELECT art_id,count(*) as favorites_count FROM cushbu_mark_user_favorites GROUP BY art_id) as b ON b.art_id=cushbu_art.id
WHERE cushbu_mark_user_favorites.user_id=1
GROUP BY cushbu_art.id
Hope this will helpful to you.
As the title states I am trying to Left Join information from table B, but then also show matching information in table A, thats not associated in table B. Below is my query but it's currently only returning the inner join.
SELECT
concat(u.firstname, ' ', u.lastname) AS clientName,
u.email AS clientEmail,
u.created_at AS signedUp,
u.refferedby AS referredBy,
concat(t.firstname, ' ', t.lastname) AS trainerWho,
t.email AS trainerWhoEmail,
concat(usr.firstname, ' ', usr.lastname) AS clientWho,
usr.email as clientWhoEmail
FROM users u
LEFT JOIN trainers t ON u.externalid = t.hashId
INNER JOIN users usr ON u.externalid = usr.hashId
WHERE u.refferedby IS NOT NULL
AND DATE(u.created_at) >= CURDATE() -7
ORDER BY(u.created_at);
1 user has multiple records in tbl_activity and there is diplay_name, I would like to concat all of them il a columns name activity_name
SELECT *
FROM `tbl_user`
INNER JOIN `tbl_user_authassignment`
ON `tbl_user`.`id` = `tbl_user_authassignment`.`userid`
INNER JOIN `tbl_activity`
`tbl_activity`.`id` AS 'activityid'
ON `tbl_user`.`id` = `tbl_activity`.`vendor_id`
WHERE `tbl_user_authassignment`.`itemname` = 'vendor'
In MySQL (which is suggested by your syntax), you can use group_concat(). I don't know which fields you care about but it is something like this:
SELECT u.*, group_concat(a.acitvityid) as activityids
FROM `tbl_user` u INNER JOIN
`tbl_user_authassignment` ua
ON u.`id` = ua.`userid` INNER JOIN
`tbl_activity` a
ON u.`id` = a.`vendor_id`
WHERE ua`.`itemname` = 'vendor'
GROUP BY u.id;
Looks like it's MySQL syntax and so you can use GROUP_CONCAT() for this purpose
SELECT tu.*,
xx.`id` AS 'activityid',
xx.activity_name
FROM `tbl_user` tu
INNER JOIN `tbl_user_authassignment` tua
ON tu.`id` = tua.`userid`
INNER JOIN (
select `vendor_id`,
`id`,
group_concat(diplay_name) as activity_name
from `tbl_activity`
group by `vendor_id`) xx ON tu.`id` = tua.`vendor_id`
WHERE tua.`itemname` = 'vendor';
http://i.stack.imgur.com/mbUTI.jpg
I want to Do a multi select in one query where one table has data from many tables.
i have four tables to combine it into a single output.
here is a image of my table.
i want to select all and don't want other data from other table just main table with name of all other tables
have tried following but its not working.
select * from project_content
left Join project_master on project_master.id = project_content.p_id
left Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left Join project_menu_master on project_menu_master.id = project_content.m_id
select * from project_content
left Join project_master on project_master.id = project_content.p_id
left Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left Join project_menu_master on project_menu_master.id = project_content.m_id
select distinct(*) from project_content
left OUTER Join project_master on project_master.id = project_content.p_id
left OUTER Join project_content_menu on project_content_menu.type_id = project_content.p_c_id
left OUTER Join project_menu_master on project_menu_master.id = project_content.m_id
select * from project_content ,project_master,project_content_menu,project_menu_master
where project_master.id = project_content.p_id and project_content_menu.type_id = project_content.p_c_id and project_menu_master.id = project_content.m_id
select pc.id as id , pm.name as pname , pmm.name as menuname , pcm.name as contentname , pc.name as name
from
project_content as pc,
project_master as pm,
project_content_menu as pcm,
project_menu_master as pmm
where
pm.id = pc.p_id
and
pcm.type_id = pc.m_id
and
pmm.id = pc.p_c_id
If I undesrtood you correctly, you want the data just from the main table, but with names instead of foreign keys from other tables? If so, then:
SELECT pc.id, pm.name, pcm.name, pmm.name, pc.name, pc.desc, pc.thumb, pc.src, pc.status
FROM project_content AS pc
LEFT JOIN project_master AS pm ON pm.id = pc.p_id
LEFT JOIN project_content_menu AS pcm ON pcm.type_id = pc.p_c_id
LEFT JOIN project_menu_master AS pmm ON pmm.id = pc.m_id
Im not an expert in SQL but you could try using UNION operator. like this:
select names
from table1
where lastname like k%
UNION
select names
from table2
where lastname like k%
This will combine the result from table1 and table2 and display UNIQUE NAMES in the result where the lastname is starting with k. so, if there is a JOHN KRAMER and JOHN KUTCHER then only JOHN will be displayed once.
If you want duplicate entries , too, then use UNION ALL
I'm not sure, that I understood your problem well, but if you need to add all records together, you should use: UNION ALL
Like:
select name from project_content
UNION ALL
select name from project_master
UNION ALL
select name from project_master
UNION ALL
select name from project_menu_master
Just be sure, that you have the same amount of columns in each select with the same type