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;
Related
I'm trying to check and order my users table who has the most origins (referals).
I have a users table, which has an 'origin' column, filled in with another users 'username' column if they refereed them, null if nobody.
Here is what I have so far, but its constantly returning 0 for the origin_count column?
SELECT username,
(SELECT COUNT(*)
FROM users
WHERE origin = username) AS origin_count
FROM users
WHERE tmp_allow_share = 1 AND approved_at IS NOT NULL;
SELECT COUNT(*) FROM users WHERE origin = username counts how many users have themselves as origin.
What you want to do is group by username and count origin (possibly distinct if you're looking for unique referals).
SELECT COUNT(origin) FROM users GROUP BY username;
You don't need a subquery to calculate origin count, rather use count directly with group by
SELECT username,
count(*) AS origin_count
FROM users
WHERE tmp_allow_share = 1
AND approved_at IS NOT NULL
GROUP BY username;
i have table
users
with id,name,type,active,...
i have another table orders
with orderid,userid,...
i want to update orders table in such a way that
UPDATE orders SET userid=(SELECT id FROM users WHERE type="some" and active=1)
but my problem is
if SELECT id FROM users WHERE type="some" and active=1 doesnt have any result
i want to use
SELECT id FROM users WHERE type="some" limit 0,1
ie the first result
i can do this easly in any language like php/python etc but i just have access to mysql server so cannot do that
but how can i do in pure sql in single query
i tried if statement but not working
Here is one method using ORDER BY:
UPDATE orders o
SET userid = (SELECT u.id
FROM users u
WHERE u.type = 'some'
ORDER BY active DESC
LIMIT 1
);
This assumes that active only takes on the values 0 and 1. If there are other values, use ORDER BY (active = 1) DESC.
Performance should be fine with an index on users(type, active, id).
Another method uses aggregation and COALESCE():
UPDATE orders o
SET userid = (SELECT COALESCE(MAX(CASE WHEN active = 1 THEN u.id END),
MAX(u.id)
)
FROM users u
WHERE u.type = 'some'
);
I would expect the ORDER BY to be a wee bit faster, but sometimes MySQL surprises me with aggregations in correlated subqueries. That said, if you have very few rows for a given type, the performance difference may not be noticeable.
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
I have a MySQL table, in which I will be storing the email_id of a user along with the user's followers' email_ids. Its columns are:
ID, USER_EMAIL, FOLLWING_EMAIL
Now while the user is logged in to his account, I want to calculate:
The number of people he is following
The number of followers he has
Calculating the number of people he's following is simple:
select count(*) from list where user_email = <logged-in email>
But how can I calculate how many people are following him in just one query?
SELECT
COUNT(CASE WHEN user_email = '<email>' THEN 1 ELSE NULL END) AS following_cnt,
COUNT(CASE WHEN following_email = '<email>' THEN 1 ELSE NULL END) AS follower_cnt
FROM
list
WHERE
'<email>' IN (user_email, following_email)
I think you just need to change the field in your WHERE clause:
SELECT count(*) FROM list WHERE following_email = '...'
To count both in one query, use subselects:
SELECT
(SELECT count(*) FROM list WHERE user_email = '...') AS following,
(SELECT count(*) FROM list WHERE following_email = '...') AS followers
is there any optimize to store the data, to achieve this functionality?
Yes. Add an index on USER_EMAIL and another on FOLLOWING_EMAIL.
So is FOLLOWING_EMAIL the email address of a person this user is following? Or an index into another table? If the former, then:
SELECT COUNT(*) from list where FOLLOWING_EMAIL="test#test.com"
Two tables:
user (id, myValue, ...)
user_preferred (id, userid, preferredValue) fk userid -> user(id)
Explanation:
user is a table of all users. user_preferred is a table of any user's preferred values. A user can be listed multiple times in user_preferred but must have different values. Query should return all users that have a myValue that matches the preferred value of the given user. $userid is the php variable of the user passed.
The Trick:
A user could have no preference, in which case there is no entry in the user_preference table. When the above query is done, I want to return every user if the given user has no preference.
Analogy:
I'm at a bar and the bartender asks me what I want to drink. I say give me everything he has that matches my preference. The first round I say I like crappy beers. So he gives me a Fosters. Second round I say I have no preference and he gives me 12 pints ranging from Bud Light to Guinness. Instead of beers, these would be users. Get it?
Query (so far):
SELECT * FROM user WHERE
IF ((SELECT preferredValue FROM user_preferred WHERE userid = $userid) IS NULL,
1,
user.myValue ANY (SELECT preferredValue FROM user_preferred WHERE userid = $userid)
)
Additional Trick:
I don't want to run "SELECT preferredValue FROM user.preferred where id = $userid" twice. Can I save the results from the first run-time and somehow use it in place of the second?
SELECT *,(SELECT preferredValue FROM user_preferred WHERE userid = $userid) AS Result FROM user WHERE
IF (Result IS NULL,1,RESULT)
To return a list of users:
SELECT o.*
FROM `user` o
WHERE o.id IN
( SELECT DISTINCT m.userid
FROM user_preferred m
WHERE EXISTS
( SELECT 1 FROM user_preferred p
WHERE p.preferredValue = m.preferredValue
AND p.userid <> m.userid
AND p.userid = $userid )
)
OR ( o.id <> $userid AND NOT EXISTS
( SELECT 1
FROM user_preferred q
WHERE q.userid = $userid
)
)