I'm trying to create a query with multiple joins, but it's not working for me.
I have the follow query statement:
SELECT DISTINCT s.per_id
, s.per_key
, s.per_disabled
, p.acc_id
, a.acc_name
, p.approved_acc_id
, p.per_time
FROM acc_permissions p
JOIN svr_permissions s
JOIN acc_general a
JOIN svr_group_permissions g
WHERE a.acc_id = p.acc_id
AND p.per_id = s.per_id
OR a.acc_per_group = g.group_id
AND a.acc_id = p.acc_id
Now if you don't want to examine this query, I understand so I will example my table structure;
First Table (includes users):
acc_general
Second table (includes permissions (linked to users)):
acc_permission
- This table includes rows that are linked to the acc_id in acc_general.
Multiple rows are possible for one unique acc_id in this table.
Third table (includes permissions (liked to groups)):
group_permissions
Now this includes rows that are linked to groups, each group has multiple rows in this table.
Inside acc_general there is a field called; acc_group_id, this is liked with the group_id inside group_permissions
So I need a query that returns all permissions from all players.
But it should not create duplicated permissions for a account.
So if I have an account that has a permission id 1 inside acc_permission and it has permission id 1 inside group_permissions it should ignore it.
It's hard to example, but i hope someone understands what I want.
Regards, Roel
join syntax for your query
SELECT DISTINCT s.per_id AS per_id,
s.per_key AS per_key,
s.per_disabled AS per_disabled,
p.acc_id AS acc_id,
a.acc_name as acc_name,
p.approved_acc_id AS approved_acc_id,
p.per_time AS per_time
from acc_permissions p
join svr_permissions s
ON p.per_id = s.per_id
join acc_general a
ON a.acc_id= p.acc_id
left join svr_group_permissions g
ON a.acc_per_group = g.group_id
Related
SELECT stock_transfer_cnf_ord.order_id, stock_transfer_cnf_ord.retailer_user_name, stock_transfer_cnf_ord.boy_user_name, stock_transfer_cnf_ord.order_status, stock_transfer_cnf_ord.order_on, users.address
FROM stock_transfer_cnf_ord JOIN
users
ON stock_transfer_cnf_ord.boy_user_name = 'manish' and role='courier'
when i run this query i get repeated data. actually i want the address from user where role is retailer
here is my two table users and stock_transfer_cnf_ord
You seem to be missing a JOIN condition between the tables:
SELECT st.order_id, st.retailer_user_name, stock_transfer_cnf_ord.boy_user_name, st.order_status, st.order_on, u.address
FROM stock_transfer_cnf_ord st JOIN
users u
ON st.boy_username = u.username
--------^ the join condition references both tables
WHERE st.boy_user_name = 'manish' and u.role = 'retailer';
I also added table aliases -- they make the query easier to write and to read.
This is the table design of an application I need to build. Kindly ignore the Role Table in the image.
Is there any method by which I can select all the records in the table transfer log , but instead of showing "User No" and "Plant No" in the result , I need to show the corresponding Employee No/Username from the user table and corresponding Plant Id from Plant Table.
Try this;)
select distinct
log.LogNo, u.EmployeeNo/Username, p.PlantId, log.StartDate, log.EndDate, log.Active
from TransferLog log
left join UserTable u on u.UserNo = log.UserNo
left join PlantTable p on p.PlantNo = log.PlantNo
Use inner join
select a.*, b.username, c.pantname
from transfer_log as a
inner join User as b a.userno = b.userno
inner join plant as c a.plantno = c.plantno
i am trying to write the Query for three things .My table structure is like that
You can see Schema at http://sqlfiddle.com/#!2/56c2d/1
I am trying to write the query in MYSQL
user:- table
user_id
user_fname
This is User tabke which will save User Information
group:- "group" and "subgroup" is maintain in same table using column "group_parent_group_id"
group_id
group_title
group_parent_group_id(INT)
This is group table which will save Group and Subgroups
user_group: table
user_group_id
user_group_user_id
user_group_group_id
This ill store both User and Group relation using their Id
I am trying to write the Query for three things. Fetching Users Groups, Subgroups
1) Query to fetch list of All Groups for User Register. Query is gelow and is giving error
Query:
select user.id, user.user_fname, group.group_id, group.group_title
from `user`
inner join user_group on user_group.user_group_user_id = user.user_id
inner join group on group.group_id = user_group.user_group_group_id
where user_group.user_group_user_id = 1 and user_group.group_parent_group_id = 0
2) I am Looking the query to fetch all subgroups(For Whom user is already Register) for Group Id 1,2 or 1
3) I am Looking the query to fetch all subgroups(For Whom user is Not Register yet) for Group Id 1,2 or 1. Ideal is for giving him randomly suggest a subgroup to add
Please Help. I am a newbie in DB :(
Your query is probably failing as you have a table called group, which is a reserved word. You can use back tics to delimit the name to get away with this (as follows) but it would be a better idea to change the table name.
SELECT user.id, user.user_fname, `group`.group_id, `group`.group_title
FROM `user`
INNER JOIN user_group ON user_group.user_group_user_id = user.user_id
INNER JOIN `group` ON `group`.group_id = user_group.user_group_group_id
WHERE user_group.user_group_user_id = 1
AND user_group.group_parent_group_id = 0
EDIT updated for queries I think the OP requires.
First query will get a list of all the groups (ones that have no parent group id) that a user (in this case id 28) is a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
INNER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id
INNER JOIN y2m_group ON y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id = 0
This query will get a list of all the sub groups (ones where the parent group id is greater than 0) that a user (in this case id 28) is a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
INNER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id
INNER JOIN y2m_group ON y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id > 0
This query will get a list of all the sub groups (ones where the parent group id is greater than 0) that a user (in this case id 28) is NOT a member of
SELECT y2m_user.user_id, y2m_user.user_first_name, y2m_group.group_id, y2m_group.group_title
FROM y2m_user
CROSS JOIN y2m_group
LEFT OUTER JOIN y2m_user_group ON y2m_user_group.user_group_user_id = y2m_user.user_id AND y2m_group.group_id = y2m_user_group.user_group_group_id
WHERE y2m_user.user_id = 28
AND y2m_group.group_parent_group_id > 0
AND y2m_user_group.user_group_id IS NULL
Please excuse any typos as not tested (with your test data there are no sub groups).
I am making a user status list of the following format "A like B's XXX". A and B are both registered users and have firstname and lastname and user id. How to join the status table with the user table twice to get the names of the two users? Thank you.
SELECT "SQACTION"."TIMECREATED",
"SQWORDLIST".*,
"SUBJECT"."FIRSTNAME" subject_fn,
"SUBJECT"."LASTNAME" subject_ln,
author.firstname author_fn,
author.lastname author_ln
FROM "SQACTION"
INNER JOIN "SQWORDLIST"
ON SQACTION.ACTION = SQWORDLIST.GUID
INNER JOIN "SQUSER" SUBJECT
ON SQACTION.SUBJECT = SUBJECT.GUID
LEFT JOIN SQDOCUMENT
ON SQACTION.ENTITY = SQDOCUMENT.GUID
LEFT JOIN SQUSER AUTHOR
ON SQDOCUMENT.AUTHORID = AUTHOR.GUID
WHERE (SUBJECT.GUID = 'B4D3BF632C0C4DB3AB01C8B284069D8F')
OR (SUBJECT.GUID IN ('67882AF3FA3C4254AF9A12CA0B0AB6E4',
'6A4B52FE233444838AACFE2AFFE4D38F',
'8CA3FB9061FF4710B51F1E398D3D1917'))
ORDER BY "TIMECREATED" DESC
This is what I have tried. Thank you.
You need to include the table name twice in the FROM clause, and use an alias so you can specify which fields from each instance of the table are used in the ON statement. You didn't provide enough details in your question to give an exact example, so here is something more general.
UserTable, with ID & Name
RegTable, with UserID, and SponsorID
select ut1.name as [User],
ut2.name as [Sponsor]
from UserTable ut1
inner join RegTable rt on ut1.id = rt.userid
inner join UserTable ut2 on rt.sponsorid = ut2.id
do you mean something like, status have two field links to user table?
select user_a.first_name as user_a_first_name, user_b.first_name as user_b_first_name, status.status_name
from status
left join users as user_a on user_a.id = status.user_from_id
left join users as user_b on user_b.id = status.user_to_id
I am creating a view for my Database , I am joing 3 tables, Users,personal_info and contact_info, if you notice I have a lot of column names in my Select statement , since i don't want to include primary keys but it seems I have an error here, take a look
CREATE VIEW `payroll`.`new_view` AS
Select employee_id,employee_password,First_Name,Middle_Initial,
Last_Name,Date_Of_Birth,Beneficiaries,Home_Number,Address,Mobile_Number,Email_Address
From USER
LEFT JOIN personal_info on idUser = idPersonal_Info,
FULL JOIN contact_info on idUser = idContact_Info
The error is
ERROR 1146: Table 'payroll.full' doesn't exist
SQL Statement:
CREATE OR REPLACE VIEW `payroll`.`new_view` AS
Select employee_id,employee_password,First_Name,Middle_Initial,
Last_Name,Date_Of_Birth,Beneficiaries,Home_Number,Address,Mobile_Number,Email_Address
From USER
LEFT JOIN personal_info on idUser = idPersonal_Info,
FULL JOIN contact_info on idUser = idContact_Info
quote it with backtics: payroll.new_view
CREATE VIEW `payroll.new_view`
Error on:
LEFT JOIN personal_info on idUser = idPersonal_Info
you need to specify which column on which table equals which one on the other table, like
SELECT a,b,c from table1
LEFT JOIN table2
on table1.a= table2.columnY
in your case:
on USER.idUser = Personal_Info.idPersonalInfo
and the same for the 3rd Join
Another thing is the Comma at the end of the line:
LEFT JOIN personal_info on idUser = idPersonal_Info ,
it doesnt belong there.