How can i do better query in mysql - mysql

I want to search a user that is not in specific group.
I have a working query in MYSQL and i need some advice and some help.
Here is my query.
SELECT id, fullname FROM users WHERE
id NOT IN (
SELECT user_id FROM group_members WHERE group_id = $groupId
)
AND fullname LIKE $serchKey
the subquery is getting all id on a group then use it as reference in WHERE clause (id NOT IN) of main query, I have no large data by now. i think if i have large data and getting all id as reference use in (id NOT IN) i think it will took large time before getting the result. what can you say?
can you give me a good suggestions for this. thanks

What you have done is the best way as I can think of with relate to the condition and code maintainability.
SELECT id, fullname
FROM users
WHERE id NOT IN (SELECT user_id FROM group_members WHERE group_id = $groupId)
AND fullname LIKE $serchKey
But there is an alternative using LEFT JOIN which is arguable on the performance. (I didnt check this SQL, please check it in your DB)
SELECT distinct u.id
FROM users u
LEFT JOIN group_members gm
ON u.id = gm.user_id
WHERE gm.user_id IS NULL
AND u.fullname like $serchkey

Related

use COUNT(*) values from one table to another

Suppose I have two tables, users and posts. Posts has the following fields, userid, postid, etc and userid can appear multiple times as one user can write multiple posts....I'm just trying sort the users table based off the # of occurrences per userid in the posts table. I can get the # of occurrences per user using this
SELECT userid, COUNT(*)
FROM posts
GROUP BY userid;
I would like to use the values under COUNT(*) column, maybe add it to my other table because then I can simply to something like this
SELECT * FROM users
ORDER BY newcolumn ASC;
but I'm having trouble doing that. Or can I do it without having to add an extra column? Hints please. Thanks
Left join is the key here!
SELECT users.userid,count(posts.userid) AS total_count
FROM users
LEFT JOIN posts on posts.userid = users.userid
GROUP BY users.userid
ORDER BY total_count DESC;
We are taking the left join on two tables with same user_id and we are counting the total number of posts per user using group by. Finally sort by count and show results.
try an left join:
select users.userid, [user fields],count(postid) as posts_count
from users
left join posts on posts.userid = users.userid
group by users.userid,[user fields]
order by posts_count desc.
You want to select users (FROM users) but you want to sort based on criteria in another table (COUNT(*) FROM posts) -- therefore you need to use a JOIN
Off-hand I can't seem to recall if "JOIN" or "RIGHT JOIN" or "FULL JOIN" is what you need if you wanted to get a cartesian product of the tables then group and aggregate on a single field, but I can avoid the need to remember with a subquery (hopefully someone will soon post a smaller and smarter answer):
SELECT users.* FROM users
JOIN (
SELECT userid, COUNT(*) as count
FROM posts
GROUP BY userid
) as subquery ON users.id = subquery.userid
ORDER BY subquery.count
Note: I haven't tested this query, but it looks good to me. Again: hopefully someone will post a better answer soon as I'm not doing my due dilligence, but you definitely need a JOIN :)
You could add a post_count column to the users table, but you would also have to update that count column every time a user creates a new post and you would have to build that logic into your application.
Otherwise, it looks like the answer from FallAndLearn will get you what you need.

SQL - Renaming fields after query

Im Running into walls with this one but im sure somebody here knows a way around
I have 2 tables for example USERS and ISSUES. In USERS they have we say 2 columns needed
id
firstname
lastname
in 'ISSUES` they have 2 columns needed
id
assigned_to_id
Im trying to compare and replace a field on output.
for example if users.id = issues.assigned_to_id
then print 'users.firstnameinstead ofusers.id`
Any help would be fantastic. cheers guys.
EDIT; i can do two queries to what i need, the first one is this;
SELECT assigned_to_id AS Name, COUNT(*) AS Issues FROM issues
WHERE `status_id`=1
OR `status_id`=2
OR `status_id`=4
OR `status_id`=7
OR `status_id`=8
OR `status_id`=9
OR `status_id`=10
GROUP BY `assigned_to_id`
and this one;
SELECT id, firstname FROM users
now if users.id = issues.assigned_to_id basically use the corresponding users firstname instead of the id
I don't see an issues.firstName column in your example so I assume you mean users.firstname
So to put into plain English what your after...
You want to return all issues and when an issue matches a user return the user name instead of the issueID?
If so then this should do it (Changed left to right as you wanted all issues I believe)
SELECT coalesce(u.FirstName, to_char(I.ID)) as UserNameOrIssueID
FROM Users U
RIGHT JOIN issues I
on U.ID = I.assigned_to_ID
The tricky part here is that firstname and ID are likely of different data types, so you have to cast the user.id to a character field in the DB appropriate syntax...
Using explicit cast (I'm also assuming Issue.ID is a numeric field if it's character then the second one below will work fine as it doesn't even need to do the implicit conversion.
SELECT coalesce(u.FirstName, cast(I.ID as char(30)) as UserNameOrIssueID
FROM Users U
RIGHT JOIN issues I
on U.ID = I.assigned_to_ID
Hoping implicit works:
SELECT coalesce(u.FirstName, I.ID) as UserNameOrIssueID
FROM Users U
RIGHT JOIN issues I
on U.ID = I.assigned_to_ID
I do not know, if this is what you need:
select
u.firstname
from
users as u,
issues as i
where
u.id = i.assigned_to_id;
You can not do that ... Only if you Alter table issues...
all you can do is this !
SELECT U.firstname FROM USERS U
LEFT JOIN ISSUES I ON U.id = I.assigned_to_id
I'll pus an update later ... with the alter table ! Stay tune !
UPDATE
IF OBJECT_ID ('tempdb..#TempTable') IS NOT NULL
DROP TABLE #TempTable
SELECT U.firstname INTO #TempTable FROM USERS U
LEFT JOIN ISSUES I ON U.id = I.assigned_to_id
SELECT * FROM #TempTable
-- ALTER TABLE ISSUES ADD firstname varchar(100) -- RUN ONLY ONCE !
UPDATE I
SET firstname = (SELECT TOP 1 U.firstname FROM USERS U
LEFT JOIN ISSUES I ON U.id = I.assigned_to_id)
FROM ISSUES I
SELECT * FROM ISSUES
I had a look over the weekend, this is what worked and did what i needed it to do. thanks for your help guys.
SELECT COUNT(*) as Issues, assigned_to_id as UserID , users.firstname as Name FROM issues
JOIN users on users.id = issues.assigned_to_id
WHERE `status_id`=1
OR `status_id`=2
OR `status_id`=4
OR `status_id`=7
OR `status_id`=8
OR `status_id`=9
OR `status_id`=10
GROUP BY `assigned_to_id`
ORDER BY Issues DESC

SQL query to exclude one to many that have a specific value?

Using MySQL, I'd like to list all users that don't have the document "liaison". It could means Users that does not have any document at all, or users that have documents, but not "liaison" in these ones.
How can I do using MySQL Query ? I can't make it work!
Here's the (simple) model
Users (id, name)
Documents (id, user_id, name, path)
The NOT EXISTS is a workable solution. As an alternative, sometimes, with large sets, an "anti JOIN" operation can give better performance:
SELECT u.*
FROM Users u
LEFT
JOIN (SELECT d.user_id
FROM Documents d
WHERE d.name = 'liaison'
) l
ON l.user_id = u.id
WHERE l.user_id IS NULL
The inline view aliased as l returns us a list of user_id that have document named 'liaison'; that result set gets outer joined to the Users table, and then we exclude any rows where we found a match (the test of l.user_id IS NULL).
This returns a resultset equivalent to your query with the NOT EXISTS predicate.
Another alternative is to use a query with a NOT IN predicate. Note that we need to guarantee that the subquery does not return a NULL, so the general approach is to include an IS NOT NULL predicate on the column being returned by the subquery.
SELECT u.*
FROM Users u
WHERE u.id NOT IN
( SELECT d.user_id
FROM Documents d
WHERE d.user_id IS NOT NULL
AND d.name = 'liaison'
)
I'd write the NOT EXISTS query like this:
SELECT u.*
FROM Users u
WHERE NOT EXISTS
( SELECT 1
FROM Documents d
WHERE d.name = 'liaison'
AND d.user_id = u.id
)
My personal preference is to use a literal 1 in the SELECT list of that correlated subquery; it reminds me that the query is just looking for the existence of 1 row.)
Again, I usually find that the "anti-join" pattern gives the best performance with large sets. (You'd need to look at the EXPLAIN output for each statement, and measure the performance of each to determine which will work best in your situation.)
The correct query you are looking for is:
SELECT
*
FROM
Users
WHERE
id NOT IN (
SELECT
user_id
FROM
Documents
WHERE
name = "liaison"
)
This will achieve the exact result you are looking for. If a specific user has no documents, it will be listed. If it has many documents, and one of those is 'liaison', it won't be listed.
If you want to search for 'liaison' in your document's name, replace name = "liaison" for name LIKE "%liaison%".
It basically says: Select all users such as there are no documents with name "liaison" pointing to it.
So, I finally came up with this solution that seems to work good :
SELECT * FROM users u WHERE id NOT IN (SELECT DISTINCT user_id FROM user_documents WHERE name = 'LIAISON') ORDER BY c.lastname, c.firstname
SELECT users.*
FROM users left join Documents
on users.id = Documents.user_id
and documents.name='LIAISON'
WHERE documents.user_id is null
select * from Users where not exists (select id from Documents where Users.id = Documents.id and Documents.name = 'liaison')
Try :
SELECT DISTINCT u.*
FROM users u LEFT JOIN documents d ON d.user_id = u.id
WHERE d.id IS NULL OR d.name NOT LIKE '%liaison%'
Remove percent signs if "liaison" is the exact name of the document.

MySQL select rows that do not have matching column in other table

I can't seem to figure this out so far. I am trying to join two tables and only select the rows in table A that do not have a matching column in table B. For example, lets assume we have a users table and a sent table.
users table has the following columns: id, username
sent table has the following columns: id, username
I want to select all rows from users where username does not exist in sent table. So, if tom is in users and in sent he will not be selected. If he is in users but not in sent he will be selected. I tried this but it didn't work at all:
SELECT pooltest.name,senttest.sentname
FROM pooltest,senttest
WHERE pooltest.name != senttest.sentname
Typically, you would use NOT EXISTS for this type of query
SELECT p.Name
FROM pooltest p
WHERE NOT EXISTS (SELECT s.Name
FROM senttest s
WHERE s.Name = p.Name)
An alternative would be to use a LEFT OUTER JOIN and check for NULL
SELECT p.Name
FROM pooltest p
LEFT OUTER JOIN senttest s ON s.Name = p.Name
WHERE s.Name IS NULL
Note that the implicit join syntax you are using is considered obsolete and should be replaced with an explicit join.
Try this SQL:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.username = users.username
WHERE sent.username IS NULL;
The better way in my opinion would be:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.id = users.id
WHERE sent.id IS NULL;
As both the id fields, would be indexed (primary key I would have thought) so this query would be better optimised than the first one I suggested.
However you may find my first suggestion better for you, it depends on what your requirements are for your application.
May be this one can help you ....
I had also the same problem but Solved using this this query
INSERT INTO tbl1 (id,name) SELECT id,name from tbl2 where (name) not in(select name from tbl1);
hope this one will solve your problem

MySQL returning results from one table based on data in another table

Before delving into the issue, first I will explain the situation. I have two tables such as the following:
USERS TABLE
user_id
username
firstName
lastName
GROUPS TABLE
user_id
group_id
I want to retrieve all users who's first name is LIKE '%foo%' and who is a part of a group with group_id = 'givengid'
So, the query would like something like this:
SELECT user_id FROM users WHERE firstName LIKE '%foo'"
I can make a user defined sql function such as ismember(user_id, group_id) that will return 1 if the user is a part of the group and 0 if they are not and this to the WHERE clause in the aforementioned select statement. However, this means that for every user who's first name matches the criteria, another query has to be run through thousands of other records to find a potential match for a group entry.
The users and groups table will each have several hundred thousand records. Is it more conventional to use the user defined function approach or run a query using the UNION statement? If the UNION approach is best, what would the query with the union statement look like?
Of course, I will run benchmarks but I just want to get some perspective on the possible range of solutions for this situation and what is generally most effective/efficient.
You should use a JOIN to get users matching your two criteria.
SELECT
user_id
FROM
users
INNER JOIN
groups
ON groups.user_id = users.users_id
AND groups.group_id = given_id
WHERE
firstName LIKE '%foo'
You don't need to use either a UNION or a user-defined function here; instead, you can use a JOIN (which lets you join one table to another one based on a set of equivalent columns):
SELECT u.user_id
FROM users AS u
JOIN groups AS g
ON g.user_id = u.user_id
WHERE g.group_id = 'givengid'
AND u.firstName LIKE '%foo'
What this query does is join rows in the groups table to rows in the users table when the user_id is the same (so if you were to use SELECT *, you would end up with a long row containing the user data and the group data for that user). If multiple groups rows exist for the user, multiple rows will be retrieved before being filtered by the WHERE clause.
Use a join:
SELECT DISTINCT user_id
FROM users
INNER JOIN groups ON groups.user_id = users.user_id
WHERE users.firstName LIKE '%foo'
AND groups.group_id = '23'
The DISTINCT makes sure you don't have duplicate user IDs in the result.