Reputation: 1
I have 2 tables Forum and Interest.
Forum Table includes Forum Title and Category.
Interest Table includes Username, Category and Interest. Interest is either 1 or 0.
How do I display a table with all Forum details sorted according to Interest for a specific user?
An Example of Result wanted with Interest column
| Forum Title | Category | Interest|
| ----------- | --------- | ------- |
| Forum1 | Category2 | 1 |
| Forum1 | Category3 | 1 |
| Forum2 | Category5 | 1 |
| Forum1 | Category1 | 0 |
| Forum2 | Category4 | 0 |
Upvotes: 0
Views: 461
Reputation: 1269663
As a note, you don't have to explicitly select the interest to do this:
select f.Forum_Title, f.Category
from Forum f
where f.username = ?
order by (select i.interest from interest i where f.Category = i.Category) desc;
You can add a second key if you want each group ordered alphabetically.
More important than showing an alternative method for looking up the interest is the ?
. Presumably, you are doing this from some application. The value for the user should be passed in as a parameter rather than as an explicit value.
Further, you should have a table of users
and then use user_id
for the foreign key relationships in all the other tables.
Upvotes: 0
Reputation: 37473
You can try using join
select Forum_Title, Category,Interest
from Forum f join Interest i on f.Category=i.Category
where username='Your specific User'
order by interest desc
Upvotes: 1