i want to make comment in article. what i need is : username, comment, and id post than every post have different comment. i have 3 table, this below is my table name and it field :
user : id_user, username
posting : id_user, id_post
comment : id_com, id_user, id_post, comment
when i finish make it with my query, i check it by comment in user 1 post using user 2 account. what happend is, comment become posted by user 1.
i use this query below :
$query=$dbc->query("SELECT
user.username,
comment.id_com,
comment.comment,
posting.id_post
FROM comment
INNER JOIN (posting INNER JOIN user ON user.id_user =
posting.id_user) ON posting.id_post = comment.id_post
WHERE comment.id_post='$_GET[id]'");
Related
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
I have 3 tables: person, image and comment. Person has fields id and name. Images are for a specific person so it has id, person_id, image_name. And comment is kind of a hybrid table because a comment can be either directly for a person or for an imaga - so it has fields id, person_id, image_id and comment.
The same table was used for both comments because the comment plugin is identical for both cases and i also need to show all comments in a live feed so it seemed easier to keep them in one table.
My problem now is tho how can i write a query that gives me all the relevant data reardless if comment is for person or for image.
Select should return a row for each comment with additional data:
comment
image_name (if comment is tied to image_id)
person_name (regardless if comment is tied directly to person or to an image)
The problem im facing is I don't really know how to select person through image if image_id is present and otherwise select person directly through person_id. Also its kind of hard to search for this kind of a solution because its hard to put it in words.
Edit, added sqlfiddle:
http://sqlfiddle.com/#!9/7fa06a/1
The thing that is currently missing is the person data for second row. That data should be available because image table has person_id but i dont know how to select it.
Solution seemed to be to join person table with both person and image using OR and depending which one is present it gets joined:
SELECT *
FROM comment
LEFT JOIN image ON image.id = comment.image_id
LEFT JOIN person ON person.id = image.person_id OR
person.id = comment.person_id
You need to make comment table as main table and then left join person and image tables.
Select * from comment c left join person p on p.id=c.person_id left join image i on i.image_id=i.id and i.person_id=c person_id
You could do that with COALESCE(). It returns the first argument that is not NULL.
SELECT comment.comment, image.name, COALESCE(person.name, image_person.name)
FROM comment
LEFT JOIN person on person.id = comment.person_id
LEFT JOIN image ON image.id = comment.image_id
LEFT JOIN person image_person ON image_person.id = image.person_id
The answer of user1985273 should more efficient, though.
I working on a secured blog where I have a specific post displayed in separate page once a user clicks read more button. This separate page includes registered users comments.The issue I have is that the name of the user who wrote the comment is not matching the actual user, its showing instead a name of a user who wrote a post. My sql query:
$comsql="SELECT c.*,u.name FROM posts p JOIN users u ON p.user_id=u.id"
." JOIN comments c on p.id=c.post_id ORDER BY commentdate DESC";
My echo:] Written by: , On: ">Edit comment | ">Delete comment
Yeah, it's what you ask:
ON p.user_id=u.id
You should have somthing like this:
ON c.user_id=u.id
In your SQL query you're matching the user of the post instead of the user of the comment. Do you have a user_id field in the comments table ?
Probably, both comments and users tables have name field. To fix this;
Change query to:
$comsql="SELECT c.*,u.name AS username FROM posts p JOIN users u ON p.user_id=u.id"
." JOIN comments c on p.id=c.post_id ORDER BY commentdate DESC";
If doesn't work, please leave a comment
I have three table tbl_user, tbl_posts, tbl_friends
tbl_user stores all the user datas.
tbl_friends stores which user are friends with other.
tbl_posts, where the users posts are stored. userid is the field where all the users id are stored. p_viewstatus field stores the view status of the posts. if the view status of the my friends posts is only by me then that posts should not shown to other users
If sam have a post with id=40 in tbl_posts. The p_viewstatus of the post is only by me then that post must only be visible to sam only.
If sam have another post with id=41 and p_viewstatus!='only by me' this post must visible to all users who are sam's friend
Tried the following query but don't know how to relate the p_viewstatus
Note: I want to fetch all of my posts and my friends posts from **tbl_posts except the p_viewstatus of my friends posts no equal to 'only by me'**
SELECT * FROM `tbl_posts` as p,`tbl_friends` as f WHERE p.`userid`=f.`userid` and p.`userid`=23
Thanks in advance.
BELOW SCREENSHOT SHOWS THE TABLE CONTENTS AFTER JOIN QUERY.
Here's how your query should look like:
SELECT * FROM `tbl_posts` as p
WHERE p.`userid`=23 OR (p.`userid` IN (SELECT `friendsid` `FROM tblfriends` WHERE `userid`=23)
AND p.`p_viewstatus`<>'only by me')
First you select the data from the table you need. After that you filter it with the user id and the ids of the friends of the user and you also tell it that you only want the posts that are not viewable only by me.
I want to create a news update system, using MySQL and PHP. But i can't get it working, having trouble with the join satement and PHP, so i can display a news update and all the comments attached (and if a valid user is logged in, the person can delete each comment individually). But it is only the JOIN statements i'm having trouble with.
I have these tables and DB schema
news_tbl (news_id, date, user (fk to users_tbl.username), headline, bodytext and picture)
users_tbl (username, email, password, usertype)
comments_tbl (comments_id, name, comment, news_id(fk to news_id))
I have tried with this:
$sqlquery ="SELECT news_tbl.*, users_tbl.*, comments_tbl.*,
COUNT(comments_tbl.comments_id) AS comments_count
FROM news_tbl
LEFT JOIN users_tbl
ON news_tbl.user = users_tbl.username
LEFT JOIN comments_tbl
ON comments_tbl.news_id = news_tbl.news_id
GROUP BY news_tbl.news_id ";
But then i can only display one comment, and i want all the comments, and i want to fecth the ID of each comments, so the user can delete each comment individually. And also i cannot get a news id, if a comment isn't written?
This should get roughly what you want,although you should always share your query and also specify exactly what you pretend to achieve.
Select ut.user_id,nt.news_id,nt.headline, nt.body,ct.comments_id,
ct.comment,count(ct.comments_id) as total
from news_tbl nt
inner join users_tbl ut on ut.user_id=nt.user_id
left join comments_tbl ct on ct.news_id=nt.news_id
group by ut.user_id,nt.news_id
good luck.