Create news update with comments, how to join tables? - php

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.

Related

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

SELECT Data from Two Separate Tables

I have two separate tables. I need to SELECT the avatar from the user table, WHERE the username equals from on the comments table. I am trying to create a comment system that displays the user's avatar next to their message.
Comments - ID|Username|From|Timestamp|Message
User - ID|Username|Avatar
$fetchto=mysql_fetch_object(mysql_query("SELECT * FROM user WHERE username='$variable'"));
I think I could display the URL to the avatar using $fetchto->avatar if I had a variable that would pull the avatar of the member making the comment from the user table.
First off your database isn't properly normalized. The comments should refer to the User by UserId, not by Username. Once you've fixed that:
select * from Comments c
join User u on u.ID = c.UserId
Until then:
select * from Comments c
join User u on u.UserName = c.UserName
Also, please stop using the mysql_ family of functions - they're deprecated.
Your query needs to have a simple join, something like this:
SELECT c.*, u.avatar
FROM comments AS c
JOIN user AS u ON c.username = a.username

SELECT command denied to user on database that doesn't exist

Been bothered with this for awhile now and i think it might be how i have the joins set up.
I have two tables. Ones is called info which contains all of a users contact information. My second table called numbers has all the phonenumbers for different users. They are related by the primary id of info to the info_id of phonenumbers. I want them to join based on this relationship and I want all the phonenumbers under phonenumbers to join into the single phonenumbers column in info. The current join i am using is this.
SELECT phonenumbers p, info i FROM i.phonenumbers
INNER JOIN p.workphone
ON i.PID=p.info_id
INNER JOIN p.homephone
ON i.PID=p.info_id
INNER JOIN p.mobilephone
ON i.PID=p.info_id
all i get is the SELECT comman is deneied to user on database workphone that isnt evena database.
table info:
PID,
firstname,
lastname,
address,
email,
phonenumbers,
table phonenumbers:
PID,
workphone,
homephone,
mobilephone,
info_id,
The syntax for a join would be nice. All the tutorials just give examples and not an explanation of what the different pieces are.
JOIN syntax is
TYPE_OF_JOIN database.table ON field = field
Since you have
JOIN p.workphone ON i.PID = p.info_id
You're actually telling the DB to look for a database named p, which contains a table workphone.
Doesn't matter that you've created an alias p up in your SELECT fields list. That's a field alias, and they NOT the same as a table alias.

One record 27 times

I have a big problem with MySQL. I want to write script like facebook newsfeed.
My query return me 27 the same records. I don't know why.
How it works?
Script displaying posts written by me, my friends or my profile.
My tables:
users:
id, firstname, lastname
friends:
friend1, friend2, status, date
wall:
update_id, author, to_profile, content, date, photos
My query:
SELECT wall.update_id, wall.author, wall.to_profile, wall.content, wall.date, wall.photos, users.*, friends.sender_id, friends.friend_id, friends.status
FROM
wall
INNER JOIN friends ON
wall.author = friends.sender1
AND friends.friend2 = '".$_SESSION['id']."'
AND friends.status = '1' OR wall.author = '".$_SESSION['id']."'
OR wall.to_profile = '".$_SESSION['id']."'
INNER JOIN users ON users.id = wall.author
ORDER BY wall.date DESC
I also want to display post written by pages which I liked.
I created tables:
pages:
page_id, page_name
page_likes:
page_id, user_id, date
and *pages_wall:**
like_id, page_id, user_id, date
How to connect this to my query? And (the most important) how to repair my query?
Thanks in advance,
Matthew
That's a lot of joining going on. Try using your JOINs just to connect the tables, and then use WHERE to cut down the results. Because as it stands, those ORs aren't working like you probably think they are, they need some () around them.
I think you need some structural changes to this database for it to work well in the future. I'd add an ID field to friends, even if just on the admin side, you're going to want to manage those records.
Also, you shouldn't be querying user.* in this query. It seems like you want to pull out every user setting... for every single wall post. This will get rid of "INNER JOIN users ON users.id = wall.author " at the end which will help. Get that information in it's on query prior to calling this wall display.
SELECT *
FROM users
WHERE users.id = wall.author

Displaying name of two users from a different table

Keeping this clean and to the point:
I have two MySQL tables, users (id, nick) and bans (id, banned, bannedBy, length).
I want to display in a table a list of bans, but instead of displaying the banned ID and the bannedBy ID, I want to display their nick.
I can use an JOIN to get the nick of one of them, in something like this:
SELECT bans.id,bans.banned,bans.bannedBy,bans.length,users.nick
FROM bans
JOIN users ON users.id=bans.banned
But then I can't get the bannedBy's nick, and vice verca.
I hope I was clear, thanks in advance for any help.
You have to join users table twice, on different keys.
SELECT
bans.id,
bans.banned,
bans.bannedBy,
bans.length,
u.nick as 'banedNick',
u2.nick as 'bannedByNick'
FROM
bans
JOIN
users u ON users.id = bans.banned
JOIN
users u2 ON users.id = bans.bannedBy
You can use two joins:
SELECT bans.id,bans.banned,b.nick as bannedBy,bans.length,u.nick
FROM bans
JOIN users u ON u.users.id=bans.banned
JOIN users b ON b.users.id=bans.bannedBy

Categories