I have three tables on MySQL:
users_data
id[PK]
name
users_permissions
userid[FK]
moduid[FK]
modules
id[PK]
name
I want to print the modules.name table for the user that has only an entry on the permissions table base on the moduid[FK].
Does anybody can help me with this? I just don't know how to do it...
select m.name
from users_data ud
inner join users_permissions p on ud.id = p.userid
inner join modules m on m.id = ud.moduid
Related
I have 3 tables:
miners with columns id and name;
users with columns id, name and password;
user_miners with columns user_id and miner_id (foreign key to those other tables)
I need to print out users and their miner names
so for example i have user with id 1, and he has miners with names f,s,t
So i would need to print out:
1 f
1 s
1 t
how do i do that using left join?
I've tried all the possible left joins but i cant seem to get it working, maybe simply because i don't understand the concept of left join
('SELECT user_mineres.user_id, users.id, miners.name
FROM user_mineres
LEFT JOIN users
ON user_mineres.user_id=users.id
LEFT JOIN miners
ON users.id=miners.name
GROUP BY user_mineres.user_id ');
foreach ($stmt as $row)
{
echo $row['user_id'] . $row['name'] . "<br>";
}
i get errors that the column names are ambiguous or other syntax errors
You don't need a group by, only the proper joins and the proper ON clauses:
SELECT u.id, m.name
FROM users u
LEFT JOIN user_miners um ON um.user_id = u.id
LEFT JOIN miners m ON m.id = um.miner_id
I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.
This question already has answers here:
How do you join on the same table, twice, in mysql?
(3 answers)
Closed 12 months ago.
I have table info with 3 rows: id,monter1,monter2. Monter1 and monter2 are id's from table monterzy (id, name, surname) that i want to join with. The problem is i cant make a proper query to pick up these values then in php. Currently i have:
SELECT i.id
, m.name
, m.surname
FROM info i
JOIN monterzy m
ON i.monter1 = m.id;
I want to expand it to also get monter2 and corresponding name and surname. I have searched on google, there were examples with AS but i have no idea how to do it. Thanks!
You need to join the same table twice with different alias names
SELECT info.id,
m1.name as m1_name, m1.surname as m1_surname,
m2.name as m2_name, m2.surname as m2_surname
FROM info
INNER JOIN monterzy m1 ON info.monter1 = m1.id
INNER JOIN monterzy m2 ON info.monter2 = m2.id
You just need to alias the tables. That is, with each join of the same table give it a different alias (name) so that the rest of the query knows which one is which. Something like this:
SELECT
info.id,
m1.name,
m1.surname,
m2.name,
m2.surname
FROM
info
INNER JOIN monterzy AS m1 ON info.monter1 = m1.id
INNER JOIN monterzy AS m2 ON info.monter2 = m2.id
Notice how each join has an AS someAlias to give the joined table a new name just for the purposes of this query. And how the rest of the query references that alias instead of the table name.
You need to use table aliases for this :
SELECT info.id, m1.name as name1, m1.surname as surname1, m2.name as name2, m2.surname as surname2
FROM info
INNER JOIN monterzy m1 ON info.monter1 = m1.id
INNER JOIN monterzy m2 ON info.monter2 = m2.id;
You can use left join
SELECT
info.id,
m1.name,
m1.surname,
m2.name,
m2.surname
FROM
info
INNER JOIN monterzy AS m1 ON info.monter1 = m1.id
LEFT JOIN monterzy AS m2 ON info.monter2 = m2.id
I have normalized tables I want to select the items that belong to the userid
I'm familiar with select syntax but I'm very weak in joins tables so I'm a bit confused on how to get the items that belong to the user should I use join ? or is there other way
this is just simple example of my tables they have more fields
..........
user
..........
userid
firstname
address
..........
items
..........
itemsid
itemName
itemDescription
..........
user_items
..........
userid(FK)
itemsid(FK)
Use two inner join
select a.*, b.*
from user_items as c
inner join user as a on a.userid = c.userid
inner join items as b on b.itemsid = c.itemsid;
Use INNER JOIN
SQL
select user.*, items.*
from user_items
inner join user on user.userid = items.userid
inner join items on items.itemsid = user_items.itemsid;
So if I read this correctly, user_items.userid = user.userid.
So you want to join, something like this.
SELECT i.itemsid, i.itemName, i.Description FROM items i JOIN users us ON ui.userid = us.userid JOIN user_items ui ON ui.itemsid = i.itemsid WHERE ui.userid = VALUE;
Replace Value with your actuall user id
I have two tables. I want to draw a sample of the first table except where the person in the first table is also in a second table. Am having trouble doing this seemingly simple query.
table users
id|name
table catuser
id|userid|catid
I have tried
SELECT u.*,c.userid FROM `users` u
LEFT JOIN `catuser` c
ON (u.id = c.userid AND c.userid <> '197')
WHERE u.id = '1'
and variations to no avail. Would appreciate any suggestions.
How abt. this:
SELECT u.*,c.userid
FROM `users` u
LEFT JOIN `catuser` c
ON u.id = c.userid
WHERE u.id = '1'
AND c.userid <> '197'
AND c.userid is null
SELECT * FROM users WHERE id NOT IN (SELECT DISTINCT userid FROM catuser)
If you want to query only users that have one or more categories, you can use a WHERE EXISTS query:
SELECT u.* FROM `users` u
WHERE EXISTS (SELECT * FROM catuser WHERE catuser.userid = u.id)
Another possibility is to do a left join, and check whether the join succeeded on checking on null:
SELECT u.*, c.* FROM `users` u
LEFT JOIN catuser c ON u.id = c.userid
WHERE c.id IS NOT NULL
If there is no corresponding row in catuser, all catuser fields will be null. By checking whether c.id is not null, you only include the rows with a category.
Note that the join may return a user multiple time, if he is in multiple categories.