I am trying to write a MySQL query that selects 10 usernames based on a condition evaluated from another table.
The result will be 10 usernames as suggestions to follow. So, I need to select 10 usernames that are currently not being followed by the logged user.
The below returns users that are already followed, so there is something wrong with it. Any idea how to fix that ?
"SELECT username
FROM users
WHERE NOT EXISTS
(
SELECT id
FROM user_followers
WHERE user_followers.user_followed_id = users.username AND user_followers.user_follower_id = ?
)
ORDER BY followers DESC LIMIT 10 "
user_followed_id - username of user being evaluated from the outer query.
user_follower_id - username of user for which the check is made (uses prepared statements)
Maybe try LEFT JOIN
SELECT *
FROM users u
LEFT JOIN user_followers uf
ON u.username = uf.user_followed_id
AND uf.user_follower_id <> ?
I hope this Helps:
SELECT username
FROM users
WHERE username NOT EXISTS
(
SELECT user_followers.user_followed_id -- I see in you code below that you use this to store the username
FROM user_followers
WHERE user_followers.user_follower_id = ?
)
AND username <> ? -- if the parameter is not the username,may be changed by the id column name of the user
ORDER BY followers DESC LIMIT 10
Related
I have following query
select * from user_profile
Now i want to add where condition check user status from other table (users)
select * from user_profile,users where users.status!=0
Please Do Not Recommend Join i following old join query
Thanks
If you don't want to use join, you have to use a subquery. I guess both tables have a column like userId:
select * from user_profile where userId in (select userId from users where status != 0)
try this using sub query
select * from user_profile where status!=(select status from users where id =? or status=?)
what you compare id or status in subquery
Assuming you have a relation column (say user_id or perhaps profile_id or something -- not sure until you share sample data as requested.) between the two tables, you can join the two table and filter the rows like this:
select *
from user_profile p
join users u on p.user_id = u.user_id
where u.status != 0;
If you want every column from both tables in your query, you can use:
SELECT * from user_profile, users WHERE user_profile.user_id = users.id AND users.status! = 0
Where I assumed you have some column(like user_id - user_profile
and id - users in my example) that links both tables together.
i have two tables user and events.
the query to be written is to find the names of users who have organized events
query 1:
select user.name
from user, events
where user.id = events.organiser_id
order by user.name;
query 2:
select name
from user
where id in(select organiser_id from events)
order by name;
why is it that "query 2" works fine but "query 1" doesn't? How to decide on when to use a subquery or a join?
A typical join syntax for mysql would be:
select distinct user.name
from user
join events ON events.organiser_id = user.id
order by user.name;
SELECT DISTINCT
U.Name
FROM USER U
LEFT JOIN
EVENTS E
ON U.id = E.organiser_id
WHERE E.organiser_id IS NOT NULL
ORDER BY U.Name
use JOIN to select all users and related events then filter with WHERE to exclude those users who do not have a link (an id in the event table); if there are more events for each user in the event table, then DISTINCT must be used (this in T-SQL)
The second query doesn't work because the column name doesn't match. You have one that says, "id in (select organiser_id". If you did this, it would work, "id in (select organiser_id as id"
I have a website that someone can choose if they want to see man or woman.
I have user, gender_preference and gender row.
SELECT gender_preference FROM users where user = 'b' LIMIT 1;
variable $g
SELECT user FROM users where gender = $g LIMIT 20;
first I select the gender preference from one user, than I can select all users that are relevant.
How can I do the same thing using just one select?
Something like this:
Select * from users where gender in (select gender_preference from users where user = $g)
You can have the in query to select the gender for the user and outer query to give users.
Well you can use the gender_preference column only for filtering if that's whay you are getting as input from end user.
SELECT `user` FROM `users`
where gender_preference = 'man` LIMIT 20;
(OR)
If you really want to combine both your query then combine the WHERE conditions like below but that's almost unnecessary as you will end up getting record for user b
SELECT `user`
FROM `users`
where gender_preference = gender
and user = 'b'
LIMIT 20;
I have a table users and I have a simple search query:
SELECT *
FROM users
WHERE username LIKE 'rya%'
LIMIT 10
I also have another table called follows which basically is a list of users being followed by other users. I modified the above query to search only within what particular user is following:
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id = 18
LIMIT 10
Most of the times, the query above only returns couple of results (because I am not search the entire users table).
What I want to do is combine the above 2 queries, to return a MAXIMUM of 10 results of usernames matching a certain string with first listing the user's following, and then search the entire table of users.
I already have a solution but it requires doing this at application level by first running the 2nd query, then first, and coming the two. Is there a way I can do all of this in 1 query?
Thanks
One way to do this is to search the entire user table, but order the results so that followers show up first. It's a little bit hard to understand what you want without schema, sample data, and output, but something like
SELECT users.*
FROM users
LEFT JOIN follows ON users.id = follows.following_id
WHERE username LIKE 'rya%'
ORDER BY follows.follower_id = 18 DESC
LIMIT 10
Notice that I used a LEFT JOIN in order to get all of the users. A different way would be to UNION the results of the two tables and wrap in a SELECT. I don't know why you would do it this way given the other option, but it would be something like
SELECT *
FROM (
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id = 18
LIMIT 10
UNION
SELECT *
FROM users
WHERE username LIKE 'rya%'
LIMIT 10
) AS u
LIMIT 10
edit
Since the second one is working for you, I thought I throw a variation up using UNION ALL, which can be faster than UNION because it does not remove duplicate rows. You would have to make sure there were no duplicates yourself, which we can do like this
SELECT *
FROM (
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id = 18
LIMIT 10
UNION ALL
SELECT users.*
FROM follows
INNER JOIN users ON users.id = follows.following_id
WHERE username LIKE 'rya%' AND follows.follower_id != 18
LIMIT 10
) AS u
LIMIT 10
I am trying to return a list of users and their last login date. I need to get something that doesn't effect my outter order by statement because its dynamically populated by parameters the users chooses to sort the list. The other thing is the table that stores the login times requires 2 fields to link to the user table a user_id and a user_type.
table_admin
field: id, name
table_logs
field: id, user_id, user_type, login_date
*table_admin.id = table_logs.user_id
table_logs.user_type needs to be "admin"*
I need to pull all the admin users from table_admin with their last login date (it also needs to work if there is no record in the log table), the user_type in the log table would be "admin".
Thank you for your time.
SELECT SQL_CALC_FOUND_ROWS admins.*,ld2.ip,ld2.login_date as last_login
FROM admins
LEFT JOIN (
SELECT * FROM log_logins WHERE user_type = "admin" ORDER BY login_date DESC
) as ld2 ON (ld2.user_id = admins.id)
WHERE 1
GROUP BY user_id
ORDER BY admins.id DESC LIMIT 0,40
This is what I have so far but it doesn't grab results if they do not have an entry in the log table.
This is a join with an aggregation:
select a.name, 'admin' as userType, max(l.login_date) as lastLoginDate
from admin a left outer join
logs l
on a.id = l.id and
l.user_type = 'admin'
group by a.name