Query Check Status from other table - mysql

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.

Related

Sql join two tables with another that has no common column

I have this subquery where I am getting the posts of users that a user is following. This is the subquery.
$query = "SELECT * FROM `Posts` WHERE UserID IN
(
SELECT Followed FROM `Follow` WHERE Follower = ?
)
ORDER BY PostDate DESC
";
// on bind_param ? will be $userID
This works fine but I also want to get the user's own posts and then data from a profiles table so I'm probably going to ditch the subquery for some sort of join. I've used inner joins before however the profile/posts table have a common id 'UserID' but the Follow table does not. Would a full join work or would I have to use an AS ?
A union might be best.
SELECT * FROM posts WHERE UserID = ?
UNION ALL
SELECT P.*
FROM posts P
INNER JOIN follow F ON F.followed = P.UserID
WHERE F.follower = ?

SQL JOIN cant get it working

I know the forum is full of this questions but i cant find the solution.
I want to join the table users from user_to_designment they both have a column with user_id the error that i get is:
select user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
LIMIT 0, 25
MySQL meldt: Documentatie
#1052 - Column 'user_id' in field list is ambiguous
I use this query:
select user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
Please some advice
In your select list prefix the user_id with the table name:
select users.user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
Both columns have user_id, SQL cannot choose between them, you must specify explicitly.
You need to specify which user_id to return in your select, eg select users.user_id or Select user_to_designment.user_id
You must clarify which user_id table column you are selecting.
select a.user_id,b.designment_id from user_to_designment b
full join users a
on b.user_id=a.user_id
This is because "user_id" field exists on both tables.
you must put table name before field name.
select user_to_designment.user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
It seems user_id exist in both the tables. so it gives the ambiguous error. We should say from which table we have to pick the column.
SELECT users.user_id
,user_to_designment.designment_id
FROM user_to_designment
FULL JOIN users ON user_to_designment.user_id = users.user_id
You may use either users.user_id or user_to_designment.user_id in your select statement. Always use tablename.columnname format. It avoids confusion.

how to write this sql query with WHERE clause properly?

what is the right way to write this sql query
select * from articles where id = 1;
select * from users where id = user_id in articles table;
my question is how to write the second sql statement properly
From your question I am unable to understand what you are really looking for. I think you need to inner join two tables. The query below will give you the result by joining both the tables and it will consider USER_ID column in ARTICLES table represents ID column in USERS.
SELECT * FROM USERS INNER JOIN ARTICLES ON USERS.ID = ARTICLES.USER_ID WHERE USERS.ID = 1;
select * from users where user_id in (select id from articles);
With additional filtering inside select from articles for example. Again, depends on the requested result.
SELECT *
FROM articles
INNER JOIN users ON articles.id = users.user_id
WHERE id = '1'
Or use it as a sub query
SELECT *
FROM users
WHERE user_id IN (SELECT ID
FROM articles
WHERE id = '1')

Conditional mysql join?

I have two tables, one table is for users, and the other table is user_interested_in_who table.
The idea is if a user is interested in another user, I will insert the ids for those two users to the user_interested_in_who table
The schema for my tables are:
Users user_interested_in_who
id id
name this_user (id from the users table)
interested_in_this_user (id from the users table)
interested_or_not (1 = yes, 0 = no)
So I want to query my tables by joining them together, my query is like this:
SELECT users.id, users.name, user_interested_in_who.interested_or_not
FROM users
LEFT JOIN user_interested_in_who
ON user_interested_in_who.this_user = 1 *//1 is the id of the current log in user*
GROUP BY users.id
The problem with this query is that the interested_or_not column all have 1. Even when the interested_or_not column have 0 in the record
My question is how can I query it to return NULL if no record is found on the user_interested_in_who table and how can I query it to return 0 in the user_interested_or_not column if the record is 0
Edit:
How can I make it return this kind of table:
table:
id | name | interested_or_not
1 jess NULL
2 erika 1
3 jil 0
4 adrian NULL
....
1050 mich 1
You need to use a LEFT OUTER JOIN but with no literal value in your ON clause.
This will return all the entries in the first table and either their match in the second table or NULL.
SELECT users.id, users.name, user_interested_in_who.interested_or_not
FROM users
LEFT OUTER JOIN user_interested_in_who
ON user_interested_in_who.this_user = users.id
There is no need to GROUP, and you don't need a WHERE for your example case (which you have showing all the values?). If you do want to limit to a User ID then modify as follows:
SELECT users.id, users.name, user_interested_in_who.interested_or_not
FROM users
LEFT OUTER JOIN user_interested_in_who
ON user_interested_in_who.this_user = users.id
WHERE users.id = 1
You need to JOIN not LEFT JOIN. JOIN will return only the records in both tables You also need a where clause instead of using the user's logged in id to create the join.
You dont need to group anything, that's why you're always getting 1.
This should do what you want.
SELECT users.id, users.name, user_interested_in_who.interested_or_not
FROM users
JOIN user_interested_in_who
ON user_interested_in_who.this_user = users.id
WHERE users.id=1;

Single MySQL query which checks another table for rows

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.