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
Related
I have a mysql select statement as below:-
select user.username, chat.from_user,chat.to_user,chat.message,chat.date_created
from chat join user on (chat.from_user or chat.to_user) in (user.user_id)
where(chat.from_user = 3 or chat.to_user = 3) and chat.chat_id IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
and here is my result
I always get username = admin. My expectation result is username will get correct from / to user.
Please help. Thank you.
SELECT
IF(chat.from_user=3, to_user
, IF(chat.to_user=3, form_user, 0)) AS username,
chat.from_user,chat.to_user,chat.message,chat.date_created
FROM chat
LEFT JOIN user fr_user ON chat.from_user = user.user_id
LEFT JOIN user to_user ON chat.to_user = user.user_id -- since you only want to show the TO_USERNAME, you can remove above line
WHERE (chat.from_user = 3 OR chat.to_user = 3) and chat.chat_id
IN
(SELECT distinct (MAX(chat.chat_id) )
FROM chat GROUP BY chat_group_id);
I think you intend:
select u.username, c.from_user, c.to_user, chat.message, c.date_created
from chat c join
user u
on u.user_id in (c.from_user, c.to_user)
where 3 in (c.from_user, c.to_user) and
c.chat_id in (select max(c2.chat_id)
from chat c2
group by chat_group_id
);
I'm trying to join three tables, after filtering one down to the most recent entry per user. However, all of a sudden I'm running into the error Duplicate column name 'username'. I need to join on this "duplicate" column. How do I fix this?
SELECT customers.id,customers.name,customers.username,customers.phone,customers.email,radcheck.value as password
FROM customers
RIGHT JOIN radcheck ON customers.username = radcheck.username
LEFT JOIN (
SELECT * FROM radacct INNER JOIN (
SELECT username,MAX(acctupdatetime) AS latest FROM radacct GROUP BY username
) as radrecent
ON radacct.username = radrecent.username
AND radacct.acctupdatetime = radrecent.latest
) as radlatest
ON customers.username = radlatest.username
WHERE radcheck.attribute = 'Cleartext-Password'
In the * you have two columns that are username. You need to qualify one or both of them. Example below:
SELECT
customers.id,customers.name,customers.username,customers.phone,customers.email,radcheck.value as password
FROM customers
RIGHT JOIN radcheck ON customers.username = radcheck.username
LEFT JOIN (
SELECT radrecent.username, latest FROM radacct INNER JOIN (
--^^^^^^^^^
SELECT username,MAX(acctupdatetime) AS latest FROM radacct GROUP BY username
) as radrecent
ON radacct.username = radrecent.username
AND radacct.acctupdatetime = radrecent.latest
) as radlatest
ON customers.username = radlatest.username
WHERE radcheck.attribute = 'Cleartext-Password'
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
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 ...
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'