I have a query that a previous employee created and is working correctly. However, I need to add a table to it called 'info' and am having a hard time with it. The 'info' table has a user column that should match the user_id column in the users table.
The info table has columns such as user (which is the user id), company, phone, fullname. The query currently emails out a to users that are registered to a specific tender. Then a log file is created and in that log file I need to display information from the info table such as company, phone, and fullname.
I've tried adding it after the first INNER JOIN like this:
INNER JOIN info
ON info.user = users.user_id
The query will run but it won't grab any info from the info table when I try to display it.
Below is the query that works:
$sql = '
SELECT users.user_id
, users.username
FROM users
INNER
JOIN registrations
ON registrations.user = users.user_id
AND registrations.tender = '.mysql_real_escape_string($tender).'
UNION
SELECT users.user_id
, users.username
FROM users
INNER
JOIN user_divisions
ON user_divisions.user = users.user_id
INNER
JOIN tender_divisions
ON tender_divisions.division = user_divisions.division
AND tender_divisions.tender = '.mysql_real_escape_string($tender).'
WHERE
users.verified = 1
GROUP
BY users.user_id
';
Usent the same format
Make sure that all user are exist in users table to show all the info data
SELECT users.user_id
, users.username
FROM info
INNER
JOIN users
ON info.user =users.user_I'd
Based on your input, this should work (I have formatted the SQL to match my style)
SELECT users.user_id, users.username, info.*
FROM users
INNER JOIN registrations ON registrations.user = users.user_id
AND registrations.tender = '.mysql_real_escape_string($tender).'
INNER JOIN info ON info.user = users.user_id
UNION
SELECT users.user_id , users.username, info.*
FROM users
INNER JOIN user_divisions ON user_divisions.user = users.user_id
INNER JOIN tender_divisions ON tender_divisions.division = user_divisions.division
AND tender_divisions.tender = '.mysql_real_escape_string($tender).'
INNER JOIN info ON info.user = users.user_id
WHERE users.verified = 1
GROUP BY users.user_id
There's no reason for it to not work if the initial query is working fine.
If it doesn't return any record, it means that you don't have a match between your user table and your info table. You can change the INNER JOIN
to a LEFT JOIN
to verify this.
Remark:
I don't like the GROUP BY
in the last query. It works with MySQL but this is not ANSI standard, and you might get into issues if your move to another DB with ONLY_FULL_GROUP_BY mode activated
You should have the same columns in the SELECT
than in the GROUP BY
. If the aim is to remove duplicates (it looks like as you don't use an aggregate function in the SELECT
) then you should use DISTINCT
instead
Worth a read : MySQL Handling of GROUP BY
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.