How can I pull multiple user info from one query? - mysql

I have an invoice table that holds the id of the user that sent the invoice (from_id) and the user that received the invoice (to_id).
I want to be able to pull both of their info from the profile table but I am unable to figure out how.
Below is the query I'm running that let's me pull info for just one user (from_id) because of the join.
SELECT jobs.title, profiles.display_name, invoice.to_id, invoice.from_id, invoice.amount
FROM (invoice)
JOIN jobs ON jobs.job_id = invoice.job_id
JOIN profiles ON invoice.from_id = profiles.user_id
WHERE `invoice_id` = '3'

You can use the same table twice. Give them different aliased, which I think makes a query more readable anyway.
SELECT
j.title,
tp.display_name as to_name, fp.display_name as from_name,
i.to_id, i.from_id,
i.amount
FROM
invoice i
JOIN jobs j ON j.job_id = i.job_id
JOIN profiles fp ON i.from_id = fp.user_id
JOIN profiles tp ON i.to_id = tp.user_id
WHERE
i.invoice_id= '3'

You can join on a table more than once - in this case you need to join on the profiles table twice - once to get information about who the invoice is from and once to get the information about the user the invoice was sent to.
SELECT jobs.title
, Profile_From.display_name AS [From]
, Profile_To.display_name AS [To]
, invoice.to_id
, invoice.from_id
, invoice.amount
FROM invoice
JOIN jobs
ON jobs.job_id = invoice.job_id
JOIN profiles Profile_From
ON invoice.from_id = Profile_From.user_id
-- You are just missing this part
JOIN profiles Profile_To
ON invoice.to_id = Profile_To.user_id
WHERE invoice_id= '3'

Related

i have two table called users and stock_transfer_cnf_ord i want to join and fetch data where role = courier

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.

SQL: count rows from 3rd table

I have 3 tables, the first table is the account_has_account1 where i store the relation between accounts and it's columns are account_id, account_id1, status where account_id is the account doing following to account_id1 and status is an enum type with values active, inactive where active indicates if the account is actually following, if it's inactive, then account stopped following.
the second table is named account_has_photos which i store the photos one account has stored in the database, so it's columns are account_id, photos_id, so i need this table to get all photos from one account which another account is following.
But all these have messages posted on them, and here is where the 3rd table comes which is named photos_has_message_photos, from this table i only need a count of all posted messages in one photo, the columns are photos_id, message_photos_id
for now my query is this:
SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id
FROM account_has_account1
JOIN account_has_photos
ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
it shows all photos from accounts on which account id 7 is following, but on my attempts on getting the total messages have failed, i thought on doing an INNER JOIN like this:
INNER JOIN (
SELECT photos_has_message_photos.photos_id, count(photos_has_message_photos.photos_id) as total
FROM photos_has_message_photos
) posts
ON(posts.photos_id = account_has_photos.photos_id)
and then i select from main posts.total, but it does not show any row, not even the photos, the result is empty at this point and i have no idea why and what to do.
the complete query is like this:
SELECT account_has_photos.photos_id as id, "photos" as type, account_has_photos.update_at, account_has_photos.account_id, posts.total
FROM account_has_account1
JOIN account_has_photos
ON (account_has_photos.account_id = account_has_account1.account_id1 AND account_has_photos.type_id = 17)
INNER JOIN (
SELECT photos_has_message_photos.photos_id, count(photos_has_message_photos.photos_id) as total
FROM photos_has_message_photos
) posts
ON(posts.photos_id = account_has_photos.photos_id)
WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"
again, i only need a total of rows which are messages from each photos found
Try this query updated inner select
SELECT ahp.photos_id as id, "photos" as type, ahp.update_at, ahp.account_id,posts.total
FROM account_has_account1
JOIN account_has_photos
ON (ahp.account_id = account_has_account1.account_id1 AND ahp.type_id = 17) INNER JOIN (
SELECT phmp.photos_id, count(*) as total FROM photos_has_message_photos GROUP BY phmp.photos_id
) posts
ON(posts.photos_id = ahp.photos_id) WHERE account_has_account1.account_id = 7 AND account_has_account1.`status` = "Active"

Getting values from multiple tables in mysql

I have three tables
user_table:
user_id
user_name
leave_type:
leave_id
leave_name
leave_taken
id
leave_id
applied_by (user_id)
approved_by (user_id)
My end result should be
Result:
leave_name
applied_by (user_name)
approved_by (user_name)
This is what I have tried and got stuck at. I'm sorry for not providing what I tried the first time I posted this question.
Option 1:
SELECT leave_type.leave_name, users.user_name as applied_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
from leaves
join leave_type on leaves.leave_type = leave_type.id
join users on leaves.applied_by = users.id
Option 2:
SELECT leave_type.leave_name, users.user_name as applied_by,
leaves.approved_by, leaves.no_of_days, leaves.leave_date,
leaves.leave_upto_date, leaves.leave_status, leaves.approved_on
FROM leave_type, leaves, users
WHERE leave_type.id = leaves.leave_type
AND leaves.applied_by = users.id
P.S. I'm new to MySQL & I'm not sure how to implement this.
I achieved your desired result with the following query. I'm not very sure if this is the best way though.
SELECT leave_type.leave_name, users.user_name AS applied_by,
(SELECT user_name FROM users WHERE id = leaves.approved_by) AS approved_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
FROM leaves
JOIN leave_type on leaves.leave_type = leave_type.id
JOIN users on leaves.applied_by = users.id;
If you need to join the same table twice, you can provide it with an alias.
In this case, the user table has been joined with two different aliases. I've made the aliases in capitals, so it's easier to find them, but of course, you can write them however you want.
SELECT
leave_type.leave_name,
APPLY_USER.user_name as applied_by,
APPROVE_USER.user_name as approved_by,
leaves.no_of_days, leaves.leave_date, leaves.leave_upto_date,
leaves.leave_status
from leaves
join leave_type on leaves.leave_type = leave_type.id
join users AS APPLY_USER on leaves.applied_by = APPLY_USER.id
join users AS APPROVE_USER on leaves.applied_by = APPROVE_USER.id

SQL join multiple times?

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

Assistance with MySQL Query - Omitting, Grouping?

Doing a bit of investigation and writing a query against a logs db.
I've joined a number of tables to bring back the data that I need, but i'd like to clean it up a bit.
The query returns all the users and which features they have enabled on their account.
Here is what i'm trying to do to clean it up:
Their is a column called 'actions' which has two states, 'added' and 'removed'
If a user feature has an action of 'removed' then I want to not show any of the rows for the same feature for that user which are also marked as 'added'
Is this possible?!
Here is what I have so far:
select users.id as site_id, users.company_name, feature_log.featurecode, feature.minimum_account as feature_type, users.account_type as site_type, account_types.name as account, feature_log.action, feature_log.time
from users
inner join
feature_log
on users.id = feature_log.siteid
inner join
feature
on feature_log.featurecode = feature.featurecode
inner join account_types
on users.account_type_INC = account_types.id
where feature.minimum_account != 0
AND feature.minimum_account > users.account_type
AND users.status = 'Y'
ORDER BY feature_log.time DESC
Thanks for any support!
So, in order to "mute" all the features, that have been "removed" at any point in time for a given user, you can add a (left) join on the following subquery:
SELECT DISTINCT users.id as siteid, feature_log.featurecode, TRUE as mute_feature
FROM users
INNER JOIN feature_log ON (users.id = feature_log.siteid)
WHERE action = 'removed'
This will be the list of features that a given user disabled at some point in time. Then in your query's WHERE clause, you'd add a filter like so:
AND NOT IFNULL(mute_feature, FALSE)
Essentially, that'd bring your whole query to be:
select users.id as site_id, users.company_name, feature_log.featurecode, feature.minimum_account as feature_type, users.account_type as site_type, account_types.name as account, feature_log.action, feature_log.time
from users
inner join
feature_log
on users.id = feature_log.siteid
left join (
SELECT DISTINCT users.id as siteid, feature_log.featurecode, TRUE as mute_feature
FROM users
INNER JOIN feature_log ON (users.id = feature_log.siteid)
WHERE action = 'removed'
) as muted_features ON (feature_log.siteid = muted_features.siteid AND feature_log.featurecode = muted_features.featurecode)
inner join
feature
on feature_log.featurecode = feature.featurecode
inner join account_types
on users.account_type_INC = account_types.id
where feature.minimum_account != 0
AND feature.minimum_account > users.account_type
AND users.status = 'Y'
AND NOT IFNULL(mute_feature, FALSE)
ORDER BY feature_log.time DESC