MyBB Community Forums

Full Version: SQL problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

It's been some time since i've been here. I stopped coding for almost a year now. But now I started again with some website and I have a problem with the database. I hope one of you can help me out.

I have for example 2 tables. One containing users:
uid, username, email, ....
And one containing book information that users uploaded in a form:
bid, title, genre, owner, ... (and owner is the uid of the user who uploaded it)

Now I try to make a list of all the books with their corresponding owners.
I'm using this query: SELECT b.*, u.username, u.uid FROM books b, users u WHERE b.owner=u.uid ORDER BY b.title ASC
I've set it so that users can choose how they want the books to be ordered (genre, title, bid, username,..., ASC, DESC). And when you click on the title, you get to a page with details on the book (found using bid). Up to here everything works as it should:

# Title Owner
1 The Lord of The Rings Bob
2 Harry Potter 1 Bob
3 Harry Potter 2 Alan

BUT if someone enters a book with the exact same title as someone else, I want it to show as ONE row and not as two, the owner column is not necessarily needed.

# Title Owner
1 The Lord of The Rings Bob
2 Harry Potter 1 Alan
3 Harry Potter 1 Bob

These last two rows should be merged into one. I don't think it's possible. But if it is, how could I do it?

Maybe I'll just need to show the titles only without anything else, but then I can't have the users sort them.
Look into GROUP BY

But sorry, I'm too lazy to read your entire post, so I can't suggest exactly what you need to do. You'll need to GROUP BY your select statement, and use aggregate functions.
For example (not sure if this is correct for MySQL)
SELECT First(b.id), b.title, First(u.username), First(u.uid) FROM books b, users u WHERE b.owner=u.uid GROUP BY b.title ORDER BY b.title ASC
That query seems to be incorrect. I think the First() function doesn't exist in mysql. Thanks for trying though