I have a Users table and a Payments table. I need a query which lists the users who DO NOT have a record in the payments table where the field PaymentCompleted=1.
These are the columns in the tables (simplified):
Users: UserID, UserName
Payments: PaymentID, UserID, PaymentCompleted
The query should select the field UserName.
select distinct UserName
from Users left outer join Payments on Users.UserID = Payments.UserID
where PaymentCompleted is NULL or PaymentCompleted != 1
SELECT UserName
FROM Users u
WHERE NOT EXISTS(Select 1
from Payments p
Where p.UserId = u.UserId
AND p.PaymentCompleted = 1)
select * from t_users T where T.userid not exists (select p.userid from t_payments t where PaymentCompleted=1).
One note: "not in" clauses can be computationally inefficient for large numbers of records. If you start seeing performance issues, you may want to do some refactoring/redesign.
Related
I have 2 tables:
1.Users (This table contains all the information of users like name, Userid, mobileno)
2.Transaction (This table contains the information of all the transaction of a user)
But the UserID is same in both the tables
I have some filter conditions like:
[ TransactionType=1 AND status=1 and (RealCash>0 or Bonus>0 or Winning>0)] which i want to apply on Transaction table
once I applied the condition i will have some UserID
Now i want that the information of the users from the Users table that have the same UserID which i've obtained from above from the transaction table
How can i do that in MYSQL ?
use JOIN : https://www.mysqltutorial.org/mysql-join/
For example:
SELECT
u.name,
u.Userid,
u.mobileno,
t.TransactionType
FROM
Users u
INNER JOIN Transaction t ON t.Userid = c.Userid
WHERE t.TransactionType=1 AND t.status=1 and (t.RealCash>0 or t.Bonus>0 or t.Winning>0)
But read carefully about other join types (left, right, cross) as you may get different results.
SELECT
name, Userid, mobileno
FROM
Users
WHERE
UserID IN (SELECT
UserID
FROM
Transaction
WHERE
TransactionType=1 AND status=1 and (RealCash>0 or Bonus>0 or Winning>0);
I want to get customer data from all the businesses with more than 1 user.
For this I think I need a subquery to count more than 1 user and then the outer query to give me their emails.
I have tried subqueries in the WHERE and HAVING clause
SELECT u.mail
FROM users u
WHERE count IN (
SELECT count (u.id_business)
FROM businesses b
INNER JOIN users u ON b.id = u.id_business
GROUP BY b.id, u.id_business
HAVING COUNT (u.id_business) >= 2
)
I believe that you do not need a subquery, everything can be achieved in a joined aggregate query with a HAVING clause, like :
SELECT u.mail
FROM users u
INNER JOIN businesses b on b.id = u.id_business
GROUP BY u.id, u.email
HAVING COUNT (*) >= 2
NB : in case several users may have the same email, I have added the primary key of users to the GROUP BY clause (I assumed that the pk is called id) : you may remove this if email is a unique field in users.
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.
The following query is taking 5.5 seconds. Since this is a simple primary key count and both tables have <5000 records, I'm quite surprised at how slow it is. Is there any workaround to increase performance?
SELECT COUNT(*)
FROM users
WHERE (SELECT COUNT(*)
FROM clients
WHERE userID=users.id)=0
I'm counting the number of users who have no clients.
Try this query
SELECT
COUNT(*)
FROM
users
WHERE NOT EXISTS
(SELECT
userID
FROM
clients
WHERE
userID=users.id)
Alternatively you can try this
SELECT
count(*)
FROM
users u
LEFT JOIN
clients c
ON
u.id = c.userId
WHERE
c.userId IS null
Create index on id columns
Hope this helps
Try this:
SELECT COUNT(*) FROM users u
left join clients c
on u.id = c.userID
where u.id not in (select userID from clients)
DEMO HERE
So I have 2 tables in a MySQL database, one for "Users" and the other for "Orders", where each entry in Orders is an order placed by a User (Some users can have multiple orders, while some might not have any). I am working on reporting queries and one of the reports I want is a list of Users that have never placed any orders. I am still a bit of a beginner when it comes to MySQL queries, so I am not sure how to do this one. The query will be placed via PHP if that makes any difference.
Here are my columns in each table (simplified):
Users:
ID
Name
Orders:
OrderID
OrderName
CustomerID (corresponds to User.ID that placed the order)
Any help here would be great. Thanks!
Use a LEFT JOIN on Orders and check whether OrderId is null:
SELECT U.*
FROM
Users U
LEFT JOIN Orders O ON(U.ID = O.CustomerID)
WHERE
O.OrderId IS NULL
SQLFiddle
SELECT ID, Name
FROM users
WHERE ID NOT IN (SELECT DISTINCT customerID FROM Orders)
So basically you'are looking for orders that are not made by that user
Select * from Orders where CustomerID = 5 //5 is the id of the custumer
If you are doing a search by name
Select * from Orders LEFT JOIN Users on Users.ID Where Users.name ='Mark'
If no results, it mean that customer never ordered anything
Try
SELECT U.*
FROM Orders a
RIGHT JOIN Users b
ON b.ID = a.CustomerID
WHERE a.OrderId IS NULL