SQL query from multiple tables with multiple JOINs - mysql

I have the following database where user account details are stored in the Users table, organisation details are linked to users through the 'UsersOrganisation_Map' junction table and user permissions are linked to users through the UsersSiteRoles_Map junction table. The database is MySQL
What I'm struggling with is how to get data from both the roles and org tables in a single query and have the results returned as if they're from a single table.
Here's an example script (that doesn't work) that will hopefully describe what I'm trying to achieve.
> select * from UsersOrganisation_Map, UsersSiteRoles_Map join Users on
> UsersOrganisationMap.userID = Users.userID join Organisation on
> UsersOrganisationMap.organisationID = Organisation.organisationID
> where userEmail = 'admin#test.com' AND userPassword = 'test' AND
> accountActive = 'YES' join Users on UsersSiteRoles_Map.userID =
> Users.userID join SiteRoles on UsersSiteRoles_Map.roleID =
> SiteRoles.roleID

This should be something similar to:
SELECT
u.*,
usr.*,
sr.*,
uom.*,
o.*
FROM
Users as u
JOIN UsersSiteRoles_Map as usr
ON u.userID = usr.UserId
JOIN SiteRoles as sr
ON UsersSiteRoles_Map.roleID = sr.roleID
JOIN UsersOrganisation_Map as uom
ON u.userID = uom.userID
JOIN Organisation as o
ON o.organisationID = uom.Id
WHERE
u.userEmail = 'admin#test.com'
AND u.userPassword = 'test'
AND u.accountActive = 'YES'

You just need to join all the five tables together like :
SELECT *
FROM Users u
-- join to get roles
JOIN UsersSiteRoles_Map ur ON u.id=ur.user_id
JOIN SiteRoles r ON um.role_id=r.id
-- join to get organisation
JOIN UsersOrganisation_Map uo ON u.id=uo.user_id
JOIN Organisation o ON uo.organisation_id=o.id
-- filter results
WHERE u.email=admin ...

Related

Query inside NOT IN statement Doctrine

I found this sentence in the code:
$dql
= <<<DQL
SELECT u FROM AppBundle:User u
JOIN u.Roles r
JOIN u.team t
WHERE u.id NOT IN (
SELECT user.id
FROM GameBundle:Goal g
JOIN g.user user
WHERE
g.objective = :objective
)
AND
r.profile = :sales_profile
AND
r.company = :company
AND
u.onlyStatus NOT IN (:status)
DQL;
I don't know how to works that query inside NOT IN sentence, help me please.
I need to know:
What return the query inside of NOT IN, (data types, so on...)
How to works a query inside of NOT IN (is posible ?)
SELECT u FROM AppBundle:User u
JOIN u.Roles r
JOIN u.team t
WHERE u.id NOT IN (
SELECT user.id
FROM GameBundle:Goal g
JOIN g.user user
WHERE
g.objective = :objective
)
this means take all the users that don't currently have 'objective' in the 'Goal' table.
Is this what you needed ?

How to get monthly postcount of phpBB users with SQL query?

I'm new to this SQL stuff, working with phpmyadmin. I already managed to get all usernames but don't know how to get postcount and especially postcounts within a defined periode of time (month). This is what i got so far:
SELECT DISTINCT
ug.`user_id`,
u.`username`,
pfd.`pf_full_name`
FROM
`phpbb3_user_group` AS ug
INNER JOIN
`phpbb3_groups` AS g ON g.group_name = 'User'
INNER JOIN
`phpbb3_users` AS u ON u.`user_id` = ug.`user_id`
INNER JOIN
`phpbb3_profile_fields_data` AS pfd ON pfd.`user_id` = ug.`user_id`
WHERE
ug.`group_id` = g.`group_id` AND u.`user_type` = 0
ORDER BY
ug.`user_id`
SELECT user_id FROM phpbb_users u WHERE u.user_type IN (0, 3, 1) AND u.user_regdate > 1648771200 AND (u.user_lastvisit > 0 AND u.user_lastvisit < 1651276800) ORDER BY u.user_regdate ASC
April add user sql

3 Table MySQL JOIN Query?

This MYSQL Query gets the users from a SugarCRM Database
SELECT * FROM `users`
I use this MySQL query to get the Email address of a User from a SugarCRM database...
SELECT email_address FROM `email_addr_bean_rel`
LEFT JOIN `email_addresses` ON `email_addresses`.id = `email_addr_bean_rel`.email_address_id
WHERE `bean_id` LIKE '1'
AND `bean_module` LIKE 'Users'
AND `email_addresses`.deleted = 0
AND primary_address = 1
I need help building a SQL query to JOIN these as my goal is to build a User List of all users in a SugarCRM Database and have there primary email address be part of there user data.
IN the EMail SQL above the part WHEREbean_idLIKE '1' the number 1 is the User ID from the Users table.
The id column is what should be used to join the Users table to this email SQL.
Any help please?
SELECT users.*
,email_address
FROM users
LEFT JOIN email_addr_bean_rel
ON email_addr_bean_rel.bean_d=users.id
AND email_addr_bean_rel.bean_module = 'Users'
AND email_addr_bean_rel.primary_address = 1
AND email_addr_bean_rel.deleted = 0
LEFT JOIN email_addresses
ON email_addresses.id = email_addr_bean_rel.email_address_id
AND email_addresses.deleted = 0
try this
SELECT u.*, ea.email_address
FROM users AS u
INNER JOIN email_addr_bean_rel AS eab ON (eab.bean_id = u.id AND eab.bean_module = 'Users' AND eab.primary_address = 1)
INNER JOIN email_addresses AS ea ON (ea.id = eab.email_address_id AND ea.deleted = 0)
GROUP BY u.id

Select multiple values from the same table

I have the following tables: joined_schedules, schedules, users
joined_schedules has: schedule_id, user_id, permission
schedules has: schedule_id, schedule_name
users has: user_id, name
permission can either be 0, 1, 2 (0 is an owner, 1 is an administrator and 2 is a regular user)
I am trying to select all of a users schedules where the permission is 2. I have no problem with this but I also need the owner of the schedule (permission = 0) in the same query. I am having trouble with this part.
The problem with this sql is that it only selects the schedule_id and the schedule_name. I also need the name of the user where their permission = 0 for the same schedule.
SELECT joined_schedules.schedule_id, schedules.schedule_name
FROM joined_schedules
INNER JOIN schedules
ON joined_schedules.schedule_id = schedules.schedule_id
WHERE joined_schedules.user_id = '$id'
AND joined_schedules.permission = 2
EDIT: I have also tried
SELECT joined_schedules.schedule_id, schedules.schedule_name, users.name
FROM joined_schedules
INNER JOIN schedules
ON joined_schedules.schedule_id = schedules.schedule_id
INNER JOIN users
ON users.user_id = joined_schedules.schedule_id
AND joined_schedules.permission = 0
WHERE joined_schedules.user_id = '$id'
AND joined_schedules.permission = 2
You can accomplish this by joining back on the joined_schedules table where permission = 0:
SELECT joined_schedules.schedule_id, schedules.schedule_name, u.name
FROM joined_schedules
INNER JOIN schedules
ON joined_schedules.schedule_id = schedules.schedule_id
AND joined_schedules.permission = 2
LEFT JOIN joined_schedules jsOwner
ON jsOwner.schedule_id = schedules.schedule_id
AND jsOwner.permission = 0
LEFT JOIN users u
ON u.user_id = jsOwner.user_id
WHERE joined_schedules.user_id = '$id'

mysql query join statement

I have a problem in a query.
i need to make a page where user can see all reservations he made with movie name, cinema name, seats code that he reserved
i reached this level
SELECT member.member_username, show_datetime, movie_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
WHERE `reservation`.`member_id`=1
i need to make a connection and get also the cinema_name from cinema table and seats_code from seat table and theater name
in fact, i need a query to give me all almost all data in my whole database
here is the schema for the DB
http://imageshack.us/photo/my-images/824/58054944.jpg/
JOIN these two tables too:
SELECT
m.member_username,
sh.show_datetime,
v.movie_name,
c.cinema_name,
t.theater_name
FROM member AS m
INNER JOIN reservation AS r ON r.member_id = m.member_id
INNEr JOIN show AS sh ON sh.show_id = r.show_id
INNER JOIN movie AS v ON v.movie_id = s.movie_id
INNER JOIN theater AS t ON t.theater_id = sh.theater_id
INNER JOIN cinema AS c ON c.theater_id = sh.theater_id
WHERE r.member_id = 1
Keep joining your tables
SELECT member.member_username, show_datetime, movie_name, c.cinema_name, t.theater_name
FROM `member`
JOIN `reservation` ON `reservation`.`member_id` = `member`.`member_id`
JOIN `show` ON `show`.`show_id`= `reservation`.`show_id`
JOIN `movie` ON `movie`.`movie_id` = `show`.`movie_id`
JOIN `theater` ON `show`.`theater_id` = `theater`.`theater_id`
JOIN `cinema` ON `theater`.`cinema_id` = `cinema`.`cinema_id`
JOIN `seat` ON `show`.`theater_id` = `seat`.`theater_id`
WHERE `reservation`.`member_id`=1