sql query
SELECT `user`.`email`,
`user`.`passwrd`,
`user`.`status`,
`useraccounts`.`Balance`,
`useraccounts`.`AccountID`, `accounts`.`AccountNo`,
wallet.Server_typ,wallet.DateBought,
wallet.LastDate,
wallet.Profit,
withdraws.walletAdrs,
withdraws.Amount,
withdraws.status AS WDStatus,
withdraws.message,
withdraws.Date
FROM `user`
LEFT JOIN `useraccounts` ON `user`.`email` = `useraccounts`.`email`
LEFT JOIN `accounts` ON `accounts`.`AccountID` = `useraccounts`.`AccountID`
LEFT JOIN wallet ON user.email = wallet.email
LEFT JOIN withdraws ON user.email = withdraws.email
WHERE user.type = 'user'
This is my query, it works fine but there are many records against an email address. Because of this, my email gets duplicate. whenever i call just email duplicate records come. I just want one email.thanks in advance
SELECT DISTINCT `user`.`email`, `user`.`passwrd`, `user`.`status`,`useraccounts`.`Balance`, `useraccounts`.`AccountID`, `accounts`.`AccountNo`,wallet.Server_typ,wallet.DateBought,wallet.LastDate,wallet.Profit,withdraws.walletAdrs,withdraws.Amount,withdraws.status AS WDStatus, withdraws.message, withdraws.Date
FROM `user`
LEFT JOIN `useraccounts` ON `user`.`email` = `useraccounts`.`email`
LEFT JOIN `accounts` ON `accounts`.`AccountID` = `useraccounts`.`AccountID`
LEFT JOIN wallet ON user.email = wallet.email
LEFT JOIN withdraws ON user.email = withdraws.email
WHERE user.type = 'user'
Using DISTINCT will provide only distinct records.
Related
I'm trying to join several tables in my database.
I need to get account information from the 'accounts' table with the latest meter history on it.
And if an account has no meter history, I want it to show 'meter' related fields as NULL.
Here's my query so far:
SELECT
accounts.id,
accounts.account_order,
acc.id AS accounts_class_id,
acc.zone,
acc.book,
acc.service_class,
acc.size,
acc.account_no AS series_no,
accounts.status,
application_address.address_line,
concessionaires.firstname,
concessionaires.middlename,
concessionaires.lastname,
mb.brand_name,
m.meter_no,
ms.meter_status
FROM
accounts
INNER JOIN
applications
ON accounts.application_id = applications.id
LEFT JOIN
application_address
ON applications.application_no = application_address.application_no
LEFT JOIN
concessionaires
ON applications.concessionaire_no = concessionaires.concessionaire_no
INNER JOIN
accounts_classifications acc
ON accounts.id = acc.account
INNER JOIN meter_history mh
ON mh.id = (SELECT id FROM meter_history mh2
WHERE mh2.account_id = accounts.id
ORDER BY mh2.status_date DESC
LIMIT 1)
LEFT JOIN
meter_status ms
ON mh.meter_status = ms.id
INNER JOIN
meter m
ON mh.meter = m.id
LEFT JOIN
meter_brand mb
ON m.meter_brand = mb.id
WHERE
acc.book = 1 AND acc.zone = 20 AND applications.status = '6' AND acc.status = '1'
This would return only accounts with meter history on it.
Where should I put my IF condition so I get accounts with no history as well, or if that is even possible with my query. Thank you!
I'm probably messing it up. But I have a WordPress system where I am trying to build an export of specific user data in MySQL.
I am expecting metadata to return NULL if the row doesn't exist, but instead it seems like it's acting like a limit. If any of the metadata doesn't exist the entire user is omitted.
SELECT
users.ID,
users.user_email,
_first.meta_value as firstName,
_second.meta_value as lastName,
users.display_name,
_gender.meta_value as gender,
_age.meta_value as age,
_nationality.meta_value as nationality
FROM kp_users
LEFT JOIN usermeta as _first ON kp_users.id = _first.user_id
LEFT JOIN usermeta as _second ON kp_users.id = _second.user_id
LEFT JOIN usermeta as _gender ON kp_users.id = _gender.user_id
LEFT JOIN usermeta as _age ON kp_users.id = _age.user_id
LEFT JOIN usermeta as _nationality ON kp_users.id = _nationality.user_id
WHERE
_first.meta_key = 'first_name' AND
_second.meta_key = 'last_name' AND
_gender.meta_key = '_user_demographics_gender' AND
_age.meta_key = '_user_demographics_age' AND
_nationality.meta_key = '_user_demographics_nationality'
So, for metadata rows where, say, the _user_demographics_age row doesn't exist the entire user is omitted from the final set of results, and I cannot for the life of me figure out why.
You need to put your others conditions in ON clause instead of Where Clause like below -
SELECT
users.ID,
users.user_email,
_first.meta_value as firstName,
_second.meta_value as lastName,
users.display_name,
_gender.meta_value as gender,
_age.meta_value as age,
_nationality.meta_value as nationality
FROM kp_users
LEFT JOIN usermeta as _first ON kp_users.id = _first.user_id and _first.meta_key = 'first_name'
LEFT JOIN usermeta as _second ON kp_users.id = _second.user_id and _second.meta_key = 'last_name'
LEFT JOIN usermeta as _gender ON kp_users.id = _gender.user_id and _gender.meta_key = '_user_demographics_gender'
LEFT JOIN usermeta as _age ON kp_users.id = _age.user_id and _age.meta_key = '_user_demographics_age'
LEFT JOIN usermeta as _nationality ON kp_users.id = _nationality.user_id and _nationality.meta_key = '_user_demographics_nationality'
Move your left join tables "where" conditions to the relative "on" conditions. At the moment your getting your null fields, but then your where condition is filtering them out because _first.meta_key is null and this doesn't equal 'firsst_name,
Move all your filters from where to left join:
LEFT JOIN usermeta as _first ON kp_users.id = _first.user_id and _first.meta_key = 'first_name'
LEFT JOIN usermeta as _second ON kp_users.id = _second.user_id and _second.meta_key = 'last_name'
-- etc.
MySQL optimizer changes left join to join, if left-joined table has conditions in where part of query (that's an approximation, there are additional rules).
I am using a post table and two type of entities school and user can add something to this table. post table has a post_from field to differentiate whether post is from user or school.
i want to write one query with join to user table or school table depending on post_from field.
SELECT *
FROM post
LEFT JOIN `user`
ON (user.id = post.uid AND post.post_type = 'user' )
LEFT JOIN school_profile
ON (school_profile.id = post.school_id AND post.post_type = 'school')
You can do this with a union :
SELECT post.*
FROM post
INNER RJOIN `user`
ON (user.id = post.uid AND post.post_type = 'user' )
union
SELECT post.*
FROM post
INNER JOIN school_profile
ON (school_profile.id = post.school_id AND post.post_type = 'school')
Alternatively, you can do this :
SELECT *
FROM post
LEFT JOIN `user` ON (user.id = post.uid)
LEFT JOIN school_profile ON (school_profile.id = post.school_id)
WHERE post.post_type IN ('user', 'shcool')
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've a SQL Query:
SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod
FROM `support_tickets_replies` r
LEFT JOIN `support_tickets` t ON (t.id = r.tid)
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid)
LEFT JOIN `users` responser ON (responser.id = r.uid)
WHERE r.tid = [something goes here]
I must check, does that ticket belongs to current user. User ID is in t.uid. When it's not that user, just returns column "error" with message "Forbidden". It's possible to do with only MySQL?
SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod
FROM `support_tickets_replies` r
LEFT JOIN `support_tickets` t ON (t.id = r.tid)
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid)
LEFT JOIN `users` responser ON (responser.id = r.uid)
WHERE r.tid = [something goes here]
AND t.uid = [User ID goes here]
This query will only turn up records that belong to the user.
If the record doesn't belong to the user, it will return nothing.