I'm new to mySQL relastionships and I'm wondering if you can help me out.
This is what I want to do:
Users Table
user_id
user_name
pass_word
permission_id
Permissions Table
p_id
permission_name
permission_type
I want to create a relationship between p_id (permissions table) & permission_id (user table) so when I query the user table it also brings through the corresponding permission name & type?
Is this possible or am I getting it all wrong?
Should I just use joins?
Thanks,
WebDevB
select * from Users u , Permissions p where u.permission_id = p.p_id;
Visual Representation of SQL Joins
You can perform a SELECT query that will return all of this data that you want.
Try this:
SELECT *
FROM users u
INNER JOIN permissions p ON p.p_id = u.permission_id
Related
I'm writing what is a pretty simple 2-step SQL Query.
I have one table called Users and another called ProfileCharacteristics.
**Users Table:**
UserId [PK]
UserName
**ProfileCharacteristics Table:**
UserId [FK]
.....(other data)
I'm trying to get access to (other data), but I only have the UserName available. So what I'm presently doing is running one SQL Query that matches the UserName to the UserId and stores the UserId value.
Then, I'm pulling all values that match to UserId in ProfileCharacteristics in a separate query. I have a gut feeling that I could combine these two queries into one, but I'm not sure how.
Any pointers?
EDIT: The start of a JOIN?
SELECT * FROM ProfileCharacteristics
INNER JOIN Users
ON ....
What you're looking for is an INNER JOIN:
SELECT pc.*
FROM ProfileCharacteristics pc
JOIN Users u ON pc.UserId = u.UserId
WHERE U.UserName = 'someuser'
A Visual Explanation of SQL Joins
Imagine the following MySQL table environment:
admins
id username
usergroups
id groupName
adminXusergroup (crosslinking table)
id adminId groupId
adminId and groupID are indexed and foreign key relations are configured accordingly (adminId points to admins.id and groupId points to usergroups.id).
All id fields are the primary keys.
I would like to retrieve all datasets from usergroups that correspond to a specific user in admins.
I have managed to do this in PHP by first fetching the according rows from adminXusergroup and then creating a list of IDs to select from admins in a second call. But even though it works, i feel like it can be optimized further, possibly by fetching all required information in a single MySQL query (joins perhaps?).
How can i fetch all datasets from usergroups that correspond to a specific dataset in admins (based on the id) by incorporating the information in the crosslinking table adminXusergroup ?
My current approach started like this:
SELECT * FROM usergroups WHERE usergroups.id=(SELECT id FROM adminXusergroup WHERE adminId="1");
But it doesn't seem right, and i am not sure as to how to apply nested selects in such a case?
I think you can achieve this using IN condition
SELECT *
FROM usergroups
WHERE usergroups.id IN
(
SELECT id
FROM adminXusergroup
WHERE adminId=1
);
Otherwise you can use an INNER JOIN query
SELECT *
FROM usergroups a
INNER JOIN adminXusergroup b
ON a.id = b.groupId
WHERE b.adminId = 1
Try This
SELECT a.id,u.*,x.* FROM admin as a
INNER JOIN usergroups as u on a.id=u.id
INNER JOIN adminXusergroup as x ON x.id=u.id WHERE u.id='userid'
I'm trying to insert rows into a table (usersteps) from the table steps for all users only if the step id does not exist.
INSERT INTO userssteps
(status,user_id,step_id)
SELECT
'0' ,
(SELECT DISTINCT id from users),
(SELECT DISTINCT id from steps)
I get the following error on the above MYSQL
#1242 - Subquery returns more than 1 row
Reason:
A new user signs up they should get all steps, if I create a new step i'd want to create it in usersteps for current users to see.
If there is a more clever way to do this i'd love to know but i'm stumped. I am also using cakePHP so if there is a special cakePHP way to help me in this i'd prefer that.
Table Structure
steps:
id
name
users:
id
username
password
userssteps:
id
user_id
step_id
status
It looks like you are trying to produce a cartesian product. http://en.wikipedia.org/wiki/Cartesian_product.
If there is no relations between the users and steps table then they cannot be joined, only multiplied.
INSERT INTO userssteps
(status,user_id,step_id)
select 0,
users.id,
steps.id
from users
inner join steps
The subquerys (SELECT DISTINCT id from users) and (SELECT DISTINCT id from steps) will return ALL the id's. In a insert clause you will need only one value (you can't have more than 1 value).
you can try to inner join the two tables by the ID
Try this way:
INSERT INTO userssteps
(status,user_id,step_id)
select 0 as status,
users.id,steps.id
from users
inner join steps
on (users.id=steps.user_id);
That way should works ;)
PS: Now the join is right.
Saludos.
I have a database table with the following information :
owner.primaryitowner,
owner.secondaryitowner,
owner.primarybusinessowner,
owner.secondarybusinessowner
The issue, is the owners are only stored as emails. There is another table I normally inner join users on users.username = owner.primaryitowner to get users.displayname so the data reads correctly. The issue is I need to do this for all 4 columns, and when I can only figure out how to connect 1 column in a query. Thanks for the help
P.S. I cannot change the database I am only a report writer.
Assuming you want all the display name for all owners, try something like this:
select u.displayname
from users u
inner join owners o on
o.primaryitowner = u.username
or o.secondaryitowner = u.username
or o.primarybusinessowner = u.username
or o.secondarybusinessowner = u.username
I have two tables. One is a table of users with a unique id field, and the other is a table of data, with a column that holds the user id of whoever generated that piece of data.
I want to do something like SELECT data,genned_by FROM datatable; but I want to replace the results for genned_by with SELECT username FROM users WHERE id = genned_by
So that the results from the query changes the userid into a username that corresponds with the other table.
I did some research and figured INNER JOIN might be what I'm looking for, but I'm left very unsure of how to use it after reading it. Help?
Try to use
SELECT d.data, u.username FROM database d INNER JOIN user u ON u.id=d.genned_by
Hope it helps you
SELECT datatable.data,users.username
FROM datatable, users
WHERE users.id = datatable.genned_by