JOIN on three tables is not working - php

I have these three tables, user, categories and files
In users, table I have a column user_id
And I have this query that shows all Categories.
SELECT
`categories`.`category_id`, `categories`.`timestamp`,`categories`.`name`,
LEFT(`categories`.`description`, 50) as `description`,
COUNT(`files`.`file_id`) as `file_count`
FROM `categories`
LEFT JOIN `files`
ON `categories`.`category_id` = `files`.`category_id`
GROUP BY `categories`.`category_id`
What I want to do is that to show only categories which are created by specific user.
Adding a WHERE user.user_id = categories.category_id gives
Unknown column users.user_id

The problem is that you're not joining users table anywhere (also, you have a typo in your WHERE statement: user.user_id, should be users.user_id).
So, I guess you want to add something like (in your FROM statement):
categories INNER JOIN users ON categories.user_id = users.user_id
And the filter by a user_id you like (using a where statement).
Cheers!

As you are not taking any extra information from user table so no need to join user table here. Below query should give you desired results.
SELECT
`categories`.`category_id`, `categories`.`timestamp`,`categories`.`name`,
LEFT(`categories`.`description`, 50) as `description`,
COUNT(`files`.`file_id`) as `file_count`
FROM `categories`
LEFT JOIN `files`
ON `categories`.`category_id` = `files`.`category_id`
where `categories`.`user_id` = 2
GROUP BY `categories`.`category_id`;

If you need to include some information from users table you definitely should join on users table also. Or maybe if you want to count all categories created by a specific user. Otherwise there is no purpose to do WHERE user.user_id = categories.category_id.
If this is not the case filter by categories.category_id.

Related

Joining 2 tables in PHP

This might be a dumb and very simple question, but I'm stuck and I've tried multiple ways already. So I have this code:
$RefLinksRAW = $GLOBALS['DATABASE']->query("
SELECT u.id, u.username FROM ".USERS." as u
LEFT JOIN ".TURNAMENT." as s
ON s.id_owner = u.id;");
But I want to also SELECT columns from TURNAMENT that corresponds to s.id_owner, how do I do that?
Basically I want to make a table that shows contents from both of those tables.
This is the TURNAMENT table, I want to make sure that 'id' from USERS table is same as 'id_owner' and also select 'wons' column
You can just add the columns you are after to the SELECT section. For example,
SELECT
u.id,
u.username,
s.somecolumn,
s.someothercolumn
FROM
users u
LEFT JOIN
turnament s ON s.id_owner = u.id;
Replace users and turnament with your actual table names, and somecolumn/someothercolumn with the columns you are after.

How to join 2 values from different tables in mysql

I'm a beginner in MySQL, so I need help.
I want to get values of name column from table role to show it together with information from users instead id_role.
How can I realize it? What should the query I write?
Can you attach the link with solve?
The picture for explanation.
Read more here about SQL JOINS
SELECT users.*, role.*
FROM users
INNER JOIN role ON users.id_role = role.id;
Try LEFT JOIN
SELECT `users`.*,`role`.name as role FROM `users` LEFT JOIN `role` ON `users`.id_role= `role`.id

Want to select data from different tables

I have two tables in database, named users(store user details) and posts(store post details). Now i want to get data from both tables. Like user_image from users and post description from post.
I am using this query
SELECT * FROM `users`AS u,`posts` WHERE u.user_id IN (SELECT user_id FROM `posts`)
But it returns duplicate data. I have 2 users and 3 posts but it returns 6 posts.
Try something like:
Select a.user_image, b.post_description from users as a join posts as b on a.user_id = b.user_id
Do an inner join & you shall get the desired result
In the above query a & b are alias for the two different tables. I you do not want to use alias you can also write it as users.user_image in your select statement.
Write the fields you want from both the tables in your select statements.
The below image will help you understand the inner join
Inner Join Circle for understanding
Use group by as below:
SELECT * FROM `users`AS u,`posts` WHERE u.user_id IN (SELECT user_id FROM `posts`) group by u.user_id
What about
Select * FROM user u right join posts p on u.id = p.user_id
?
if you want to get data from both tables you need to use joins.and you make sure the two tables are interlinked by primary keys
so use this can help
select user_image,post_description from users join posts on users.user_id=posts.user_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)

PHP /MySQL - *-to-Many Relationships

So, I understand how the relationships work in mysql but I'm having a hard time figuring out how its implemented in my code.
For example, say I have the 3 tables.
Table 1: users - user id, username, user city
Table 2: categories - category id, category name
Table 3: user_categories - user id, category id
If I were to query the database for every user that was in a particular city and list them out with the all of the categories they belong to... How would I do this? Would I need to loop through the results and do a separate query for each user, then list the results? Or, is there some magic query that will return a multidimensional array?
I believe the above would be many-to-many, correct me if I'm wrong....
EDIT In the user_categories table, a user can contain more than 1 category, I'm trying to figure out how to return all of them
Thanks!
You're absolutely right, it is a many-to-many query.
And from what I understand, what you're looking for is the ability to have some kind of hierarchical result to display, meaning for one user, have an array of all the categories he's assigned to...
Couple of things you could do:
Option 1: Query the users table:
SELECT u.user_id, u.username, u.user_city WHERE city = 'somecity';
From the results, get all the user_id's that match, put them in an array.
array(1,3,4,5)
Then execute a query by joining the 2 tables categories and user_categories, and passing the array as a comma separated list in a where in:
SELECT user_categories.user_id, categories.category_name
FROM user_categories INNER JOIN categories ON user_categories.category_id = categories.category_id
WHERE user_categories.user_id IN (1,3,4,5)
This will give you a list of user-id, category name that you can use in your script with the previous results to build your result set
option 2: my preferred, use MySQL's GROUP_CONCAT(http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat).
SELECT users.user_id, users.user_name, GROUP_CONCAT(categories.category_name) AS categories
FROM users
INNER JOIN user_categories ON users.id = users_categories.user_id
INNER JOIN categories ON user_categories.category_id = category.id
WHERE user.user_city = 'somecity'
GROUP BY user.user_id
This will return something like:
user_id username categories
1 u1 cat1, cat2, cat3
2 u2 cat1, cat3
You can specify the separator by using SEPARATOR in group_concat.
You need to JOIN the tables.
If I were to query the database for every user that was in a particular city and list them out with the all of the categories they belong to
SELECT *
FROM users
INNER JOIN user_categories
ON (user_id)
INNER JOIN categories
ON (category_id)
WHERE ...
You could try:
SELECT u.user_id, u.username, u.user_city, c.category_id, c.category_name
FROM users u
INNER JOIN user_categories uc ON u.user_id = uc.user_id
INNER JOIN categories c ON uc.category_id = c.category_id
WHERE u.user_city = 'Cityname';
I haven't tested this, and there might be a more efficient way to do it, but it should work.
If you are unfamiliar with joins in mysql, check this out.

Categories