how to write this sql query with WHERE clause properly? - mysql

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')

Related

Query Check Status from other table

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.

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 = ?

How to output a mixture of data from 2 mySQL tables when joined columns have similar names?

What I want to achieve is to show all the published articles and all the published questions of a specific user_id in one loop ordered by timestamp. In simple words, to show everything mixing articles and questions.
My database structure is as below, and I have put also the profiles table.
My wrong sql query is :
SELECT *
FROM articles
JOIN questions ON articles.user_id = questions.user_id
WHERE articles.user_id = '38'
AND questions.user_id = '38'
AND questions.publish = '1'
AND articles.publish = '1'
ORDER BY questions.timestamp DESC
Articles table
id
publish
user_id
user_name
article_title
article_content
article_category
timestamp
Questions table
id
publish
user_id
user_name
question_title
question_content
question_category
timestamp
Profiles
user_id
Just use a join, an outer join will get all matching records from both table:
SELECT *
FROM articles
FULL OUTER JOIN questions ON articles.user_id = questions.user_id
WHERE articles.user_id = '38'
ORDER BY questions.timestamp DESC

Sort multiple Mysql queries by common field

I'm new at this and don't comprehend Mysql very well. I'm trying to retrieve ALL media uploaded by a certain user, and sort it all by the date it was uploaded. Problem is, the media is in 3 different tables. Can someone provide me with a good solution?
$sql1 = "SELECT * FROM videos WHERE user_id = $user_id";
$sql2 = "SELECT * FROM photos WHERE user_id = $user_id";
$sql3 = "SELECT * FROM audios WHERE user_id = $user_id";
SORT DESC BY $result['upload_date'];
You want to create a JOIN. If you only want rows where there are existing relationships, you can use an inner join, set up like this:
SELECT *
FROM videos v
JOIN photos p ON p.user_id = v.user_id
JOIN audios a ON a.useR_id = p.user_id
ORDER BY upload_date DESC;
The above will select all columns, which may have some repeated things. For example I believe user_id will show up once for each table you joined, so I would narrow down your select clause.

SQL query, which one is more sufficient?

I have a database with three tables:
post
user_id
friendship
user_id
friend_id
status
I want to select all posts whose author (user_id) is current_user's friend
I am wondering which one of the following statement is more efficient? (1 is current user's id, 2 is the code for being friend)
1)
SELECT * FROM posts
INNER JOIN friendships AS fs
ON fs.user_id=1 AND fs.friend_id=posts.user_id AND fs.status=2;
2) query twice
SELECT id FROM friendships
WHERE user_id = 1 AND status = 2
# Assuming get (1,2,3)
SELECT * FROM posts WHERE user_id IN (1,2,3)
Can anyone tell me which is faster? Because I do not have enough data, I can not test....
Neither. Join your tables in the same order you would access them as a human:
SELECT p.*
FROM friendships f
JOIN posts p ON p.user_id = f.friend_id
WHERE f.user_id = 1
AND f.status = 2
Here an index will be used based on the WHERE clause to find rows in friendships, which will then be used to access posts via an index.
Make sure there are indexes on posts.user_id and friendships.user_id.