How to join 2 values from different tables in mysql - php

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

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 all three tables in MYSQL?

I have a MySQL database that has three tables holding information about uploaded photos by users. I have a PHP page that displays all the photos in the database (tbl_uploads) and who uploaded them (tbl_users). To display the photos and who uploaded them I have a join in the MySQL query.
SELECT *
FROM tbl_uploads, tbl_users
WHERE tbl_uploads.user_id = tbl_users.user_id
ORDER BY date DESC
I now want to join a third table tbl_collab to the MySQL query that allows me to display all the users that collaborated with the photo (a form allows them to post the $file and their $user_id to tbl_collab). I guess I need to add a join from tbl_uploads.file with tbl_collab.user_id but I'm not sure how.
tbl_users
|//**user_id**//|int(11)|No|
|user_name|varchar(25)|No|
|user_email|varchar(60)|No|
|user_password|varchar(255)|No|
|joining_date|datetime|No|
tbl_uploads
|//**id**//|int(10)|No|
|file|varchar(100)|No|
|type|varchar(30)|No|
|size|int(11)|No|
|user_id|int(11)|No|
|user_name|varchar(25)|No|
tbl_collab
|//**id**//|int(11)|No|
|user_name|varchar(100)|No|
|user_id|int(11)|No|
|file|varchar(255)|No|
I have been trying your various suggestions and I can't really get them to work as I would hope so I have made a mysql fiddle that might be help me.
The problem is that when I loop through the rows that the query throws up in PHP I ether get just the rows where there is join with tbl_uploads.file and tbl.collab.file or I get the multiple rows duplicating themselves.
I'd suggest preferring ANSI SQL syntax for joins (over mentioning multiple tables in the "from" clause) as once the queries get complex I find the ANSI syntax easier to follow. Using that syntax, joining multiple tables is no big deal. e.g.,
SELECT uploads.<column>, users.<column>, collabs.<column>
FROM tbl_uploads uploads
JOIN tbl_users users ON users.user_id=uploads.user_id
JOIN tbl_collabs collabs ON collabs.file=uploads.file
ORDER BY uploads.date DESC
(Note, replace <column> above with the names of columns you want to select from the respective tables, using AS syntax to provide unique names where necessary.)
Consider that you will probably want to create indexes over the fields in the join conditions for performance if you expect the database will become large. You may also want to use left joins when joining, e.g., tbl_collabs if it is possible an upload will have no collaborators, otherwise the query will return no data if there are no matching rows in tbl_collabs.
The first thing to do is to normalize your data. If you look closely, username appears in all three tables. It shouldn't. It belongs only in the users table. Then your other tables need to have a user_id field instead of the username.
tbl_uploads
|//**id**//|int(10)|No|
|file|varchar(100)|No|
|type|varchar(30)|No|
|size|int(11)|No|
|user_id|int(11)|No|
tbl_collab
|//**id**//|int(11)|No|
|user_id|int(11)|No|
|file|varchar(255)|No|
In both cases the user_id is a foreign key to the id field in the users table. Now we have something consistent to join on.
SELECT * FROM tbl_uploads
INNER JOIN tbl_users ON tbl_uploads.user_id = tbl_users.user_id
INNER JOIN tbl_collab ON tbl_collab.file = tbl_uploads.file
Whether you should use INNER JOIN or LEFT JOIN depends on exactly what you need to do with your data, but INNER JOIN seems more appropriate based on information provided.
Update: As #drew pointed out, none of your tables have a column named date did you perhaps intend to sort by tbl_users.joining_date?
Seems the join is on file to me
SELECT *
FROM tbl_uploads
inner join tbl_users on tbl_uploads.user_id = tbl_users.user_id
inner join tbl_collab on tbl_collab.file = tbl_uploads.file
ORDER BY date DESC
You can just add another join condition. Also, note that implicit joins (having multiple tables in the from clause) isn't considered a good practice, and you should probably use explicit join clauses:
SELECT *
FROM tbl_uploads up
JOIN tbl_users us ON up.user_id = us.user_id
JOIN tbl_collab c ON c.user_id = up.user_id
ORDER BY date DESC

JOIN on three tables is not working

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.

Mysql join query retrieving profile images of users with ids from other table

I'm having trouble with a join query, my issue is as follows.
Table: battles
Fields: id,attacker_id,defender_id
Table: users
Fields: id,profile_image
I would like to do a query to retrieve a battle and get the profile images as well from the other table.
Is there a way to do this in a single or do I have to do more than one?
Thanks in advance.
I wanted to wait a while to see if you had any attempt or if you will answer my first question to know if I understood the problem. But maybe you don't have a starting point. Try something like:
SELECT
a.profile_image as attacker_profile_image,
d.profile_image as defender_profile_image
FROM
`battles` b
LEFT JOIN
`users` a
ON
b.`attacker_id` = a.`id`
LEFT JOIN
`users` d
ON
b.`defender_id` = d.`id`
the problem here is the fact that you need to join with the users table twice, so you will need to create aliases for the columns you plan to use
This query will fetch the two images only, you will need to add the extra fields

cross referencing 2 tables and getting results

I have 2 mysql tables
one has a list of user ids that are associated with a city. ie "Fort Lauderdale" but the user id is actually in a column called entity_id and the city is in a field called field_city_value.
This query brings back all of the entity_ids in "Fort Lauderdale"
SELECT entity_id
FROM `field_data_field_city`
WHERE `field_city_value` LIKE 'Fort Lauderdale'
and then this query brings back the mail for the user id
SELECT mail
FROM `users`
WHERE `uid` =42
I want to combine the 2 and get all of the mails for all of the user ids that match Fort Lauderdale.
Use a join statement.
http://dev.mysql.com/doc/refman/5.0/en/join.html
Heres the long winded way of doing it.. Untested, no mysql access on this box.
SELECT field_data_field_city.entity_id,users.mail FROM users,field_data_field_city WHERE field_city_value LIKE 'Fort Lauderdale' AND field_data_field_city.entity_id = users.uid
Or
SELECT * FROM field_data_field_city city INNER JOIN users user on city.entity_id=user.uid
Can we assume that users.uid is the same as field_data_field_city.entity_id? If that's the case you'll want to look into using MySQL joins
You can try a subselect:
SELECT mail FROM users WHERE uid IN (SELECT entity_id FROM field_data_field_city WHERE field_city_value LIKE 'Fort Lauderdale')
This should do it. You might want to change it a little bit for duplicates or similar, but this simple query should work
SELECT u.mail
FROM users u
LEFT JOIN field_data_field_city fdfc ON fdfc.entity_id = u.uid
WHERE fdfc.field_city_value LIKE 'Fort Lauderdale'

Categories