Subquery selecting over a select - php

I have a table users structured as follow:
userUID (int), userName (varchar), userImage (varchar)
And a table 'posts' structured as follow
postPID (int), postUID (int), postMessage (char)
Now, the postUID correspond to the author UID.
When i SELECT the posts, i'd like to also select the userName and userImage field corresponding to the postUID.
How could i do that?

SELECT p.*, u.userName, u.userImage
FROM users u
JOIN posts p
ON p.postUID = u.userUID

Select
p.postPID,
p.postUID,
p.postMessage,
u.userName,
u.userImage
From posts p
Join users u
On p.postUID = u.userUID
It's better to name your columns instead of selecting * as has been discussed on here in numerous threads.

It depends on how many records are returned. If not sure it is good to have top 10 or top 100 so that it only returns that many rows.

Related

SQL: How do I filter (WHERE) a joined (ON) table in SQL?

I have two tables (users and posts) and I want to write out all posts (among other things) by one user. I'm thinking I should use a JOIN and WHERE but I get an error for using WHERE.
This is my code:
SELECT username, post, joinDate, title, info FROM users
WHERE userId='18'
JOIN posts ON users.userId=posts.userId
ORDER BY date DESC
I'm new to this and perhaps there is a better way but I can't figure it out atm.
Thankful for all answers!
The JOIN clause comes before the WHERE clause, after the FROM clause. First you join together all the tables you need, then you do your filtering with WHERE. Like this:
SELECT username, post, joinDate, title, info
FROM users
JOIN posts ON users.userId=posts.userId
WHERE users.userId='18'
ORDER BY date DESC
try like below
SELECT u.*,p.*
FROM users u JOIN posts p ON u.userId=p.userId
WHERE u.userId=18
ORDER BY date DESC
where will be after join and 18 is int datatype value so not need single quote for this and use alias for avoiding ambigous column name
SELECT username, post, joinDate, title, info
FROM users
JOIN posts ON users.userId=posts.userId and users.userId='18'
ORDER BY date DESC
if userId is in users table then YES you can use where userId='18'.
If userId is in posts table then it should be userId='18' be in join part.

SQL - Return books that user owns

I'm doing I'm having a bit of a problem performing a query in my university project. I got a website where users share the books that they've read and I have a page where the user can view the books he has added (the books he owns).
For that I believe I need the logged user's id, which I store in a session PHP variable, the user id is in table users.
The information about the books is stored in a table books and it has its own id primary key.
Then, to show who owns what I have a table owns_book (id, u_id, book_id).
Right now for testing I've got 26 books total, 25 of them are added by a user with id of 57 and 1 book by user with id of 49.
When I run this query:
SELECT id, title, author, category, cover, added, user_id FROM books, users
WHERE user_id=49 AND id IN(SELECT book_id FROM owns_book)
AND user_id IN(SELECT u_id FROM owns_book)
And the result is a mess, I don't get the expected one book, I also get books added by the other user.
Can someone please show me the correct query that I need or if I need to change the structure of my tables? Thanks.
EDIT:
users(user_id, ...)
books(id, title, author, publisher, published, cover... )
owns_book(id, u_id, book_id)
It looks like you're looking to to grab everything from your books table that is owned by a specific customer. If that's the case, you can try
SELECT * FROM books
JOIN owns_book
ON books.id = owns_books.book_id
WHERE owns_book.user_id = 49
This will select all of the props from your books table then joins the tables based on on the ID of the book being equal to the book_id of the owns_book. Lastly, add the parameter - you only want to see user_id = 49.
You can simplify this query and use a LEFT JOIN...
SELECT books.id, title, author, category, cover, added, users.user_id
FROM users
LEFT JOIN owns_book on owns_book.user_id = users.user_id
LEFT JOIN books on books.id = owns_book.id
WHERE users.user_id=49
This links the user_id and lists any books owned by this user_id ( the ON bit of the JOIN). The WHERE clause just limits to listing records for the user_id your after.
If in the main list of columns, there is a column on multiple tables ( like user_id) then prefix it with the table name to allow the database to detect which column you want to use (even though they may be the same value).
You could also use inner join to join the tables users and books with the owns_book table:
SELECT id, title, author, category, cover, added, user_id
FROM owns_book
INNER JOIN users ON users.id = owns_book.u_id
INNER JOIN books ON books.id = owns_book.book_id
WHERE users.user_id=49

MySQL INNER JOIN with different column names

Okay, so I have two tables, a news table and a users table. They are set up as below:
news:
id title user_id contents
users:
id username password
When I run this query:
SELECT news.title, users.username, news.contents FROM news INNER JOIN users ON news.user_id=users.id WHERE news.id=:id
I can only access the user id using $query['id']. So it appears that the join is using the column names from table2, although I want them to map it to the column names of table1. So even though the column is called id in the users table, I want to access it under user_id, since I specified that in the JOIN query.
I need this because if I ever need the id from table1, they would both be mapped to the column called id and I would only be able to get the id of the users table.
So is there any way to do this? Access the column from table2 under the name of the column in table1?
In MySQL what you specify in the select statement is what it is called in the result set, so you need to tell it what to give you. you do that with the AS command
SELECT users.id AS user_id ... FROM ...
That way you can call it whatever you want
or grab the user_id from news with
SELECT news.user_id ... FROM ...
SELECT users.id AS id1, news.id AS id2, news.title, users.username, news.contents
FROM news INNER JOIN users ON news.user_id=users.id WHERE news.id=:id
echo $query['id1'] . $query['id2'];

mysql Search data from multiple table

I have 2 tables:
user
ID | Name | Class
Category
ID | user_id | cat_id
If user inputs data from a text field how do I search data from both tables?
You will need a basic query with joins. Something like this:
SELECT * FROM user u
LEFT JOIN category c ON c.user_id = u.id
WHERE ...
SELECT * from user, category
WHERE user.id=[text field]
or category.user_id=[text field]
or category.cat_id=[text field]
You need to join the two tables together.
Select *
from User , Category
where user.id = Category.user_id
Basicly you are linking the two together by the based upon the user_id that they both share. This way you get the information back from both tables.
Here is a link to help you understand the concept. http://www.w3schools.com/sql/sql_join.asp
Try this way
SELECT * FROM user
LEFT JOIN category ON category.user_id = user.ID
WHERE user.Name LIKE '%lorem%'
Try it
you need the time of insertion use mysql_insert_id for user_id to table2.
In the selection time use JOIN in mysql
eg
"select * from tb1,tb2 where tb1.ID=tb2.user_id and where tb1.ID='userid' "
for a specific user
Even you can do like this:
SELECT * from user
LEFT JOIN category ON user.id = category.user_id
WHERE text_field IN (user.id,category.user_id,category.cat_id)

Using the MySQL select statement

I would like to select the posts from users based on who the logged in user is following. What would I need to do? How do I use two different tables with one SELECT statement? I don't even know where to start.
I have 3 tables:
users
posts
followers
Thanks.
SELECT p.*
FROM followers f
JOIN posts p
ON p.author = f.following_id
WHERE f.user_id = $logged_in
ORDER BY
p.post_date DESC
I had to make up the field names as you haven't provided them.
Selecting from two tables is done using JOINs
http://dev.mysql.com/doc/refman/5.0/en/join.html
basically you select from two tables and define JOIN condition.
Assume you have two tables:
users with columns: user_id, user_name, online_state
posts with columns: post_id, user_id (user who posted this post), title, message
SELECT p.title, p.message FROM users u JOIN posts p ON u.user_id = p.user_id WHERE u.online_state = 'online'
join condition should be after ON, non-join condition after WHERE
I would go with the Join query as Quassonoi suggested in his answer, If you want to try an alternate solution, you can do it with subquery like this
SELECT P.PostId,P.Title,P.Body
FROM Post P WHERE P.CreatedById
IN (
SELECT FollowerID from Followers WHERE USER_ID=3
)
Replace 3 with the current user id. Assuming your table structure is something like this.
POST
PostId (int)
Title
Body
Followers
UserId (int)
FollowerId (int)

Categories