In my MySQL database, I have two tables, item and users. In the item table I have two columns called created_by and created_by_alias. The created by alias column is fully populated with names but the created_by column is empty. The next table I have is the users table. This has the id and name columns inside of it.
I would like to know whether it is possible to use MySQL to match the created_by_alias in the item table with the name column in the users table, then take the id of the user and put it into the created_by column.
I was thinking some sort of JOIN function. Any ideas would be greatly appreciated.
Actually, you are indeed in the right direction - MySQL has an update join syntax:
UPDATE items
JOIN users ON users.name = items.created_by_alias
SET created_by = items.id
Try this:
UPDATE item i
INNER JOIN users u ON i.created_by_alias = u.name
SET i.created_by = u.id;
This should work:
UPDATE items
SET created_by = (SELECT u.id
FROM users u
WHERE u.name = items.created_by_alias)
The following should work in MySQL
UPDATE item
INNER JOIN users
ON item.created_by_alias = users.name
SET item.created_by = users.id
Of course you should also check that all users names are unique...
Related
I have a table containing the columns user_id and lastreply_by_user_id and I'm looking for a way to match their columns with their username from the user table with id.
SELECT
table_threads.*,
table_users.username
FROM
threads AS table_threads
INNER JOIN users as table_users ON table_threads.user_id = table_users.id
OR table_threads.lastreply_by_user_id = table_users.id
WHERE
table_threads.category_id = :category_id
This is the code I have right now but this at the moment creates duplicate entries instead of one merged.
Just started with SQL recently and found the JOIN commands maybe it isn't the right one just point me in the right direction and I'll be thankful.
You need two joins to the same user table for that: one to connect by user_id, and one more to connect by lastreply_by_user_id. Use table aliases for disambiguation:
SELECT
t.*,
u.username as user_name,
r.username as last_reply_name
FROM
threads AS t
INNER JOIN users as u ON t.user_id = u.id -- actual user
INNER JOIN users as r ON t.lastreply_by_user_id = r.id -- reply user
WHERE
table_threads.category_id = :category_id
I need to find out how to pull user names into a VIEW from a different table. I have 3 tables, User, Lead, and Lead_detail. In the users table there is an ID field which is stored in the Created_By field in the Lead table. In the Lead table I have an ID field that is stored in the Lead_detail Lead_ID field.
I have created a VIEW to the Lead_detail table which pulls all the info I need but I have found that I don't have the users name in that VIEW so I need to ALTER my view to add in the users names per lead but I am having trouble with the statement.
Before altering the VIEW I wanted to try a SELECT statement to see if I get any data,
SELECT * FROM Lead_detail
JOIN Lead
ON Lead_detail.lead_id = Lead.id
WHERE Lead.Created_by = Users.ID
But this didn't work. What would be a correct statement so that I can pull the users names into the Lead VIEW?
I think you missed a join to the Users table:
SELECT
*
FROM
Lead_detail
INNER JOIN Lead
ON Lead_detail.lead_id = Lead.id
INNER JOIN Users
ON Lead.Created_by = Users.ID
For my tables users and assigned_roles I want to set users.confirmed = 0 except where users belongs to assigned_roles.
assigned_roles table
ID| USER_ID| ROLES
SQL
UPDATE users, assigned_roles
SET users.confirmed=0
WHERE users.id != assigned_roles.user_id
I know my last line is wrong. What is the correct way to do the query to get it to run on all users except the ones with assigned roles?
You are close. Just need a subquery in that WHERE clause:
UPDATE users
SET confirmed=0
WHERE id NOT IN (SELECT user_id FROM assigned_roles);
You can also do this with join . . . but left join:
UPDATE users u LEFT JOIN
assigned_roles ar
ON u.id = ar.user_id
SET u.confirmed = 0
WHERE ar.user_id IS NULL;
LEFT JOIN and NOT EXISTS are better for this query because of the way they handle NULL values of assigned_roles.user_id.
I have a table called player_league that looks like this:
The userID and leagueID are foreign keys referencing columns in the users and leagues table respectively.
I want to query the database to get all the users who would have a leagueID of 1 from the player_leagues table.
This query
SELECT leagueID from users join player_league pl WHERE pl.leagueID = 1
returns data that looks like
Being that I am new to mySQL and SQL in general, I was wondering what I was doing wrong? I believe it has something to do with my joins but it may have been how I set up my tables, I'm just not sure. There is no leagueID in the users table, so I'm not sure why each user would have a leagueID of 1.
You're missing the on part of the join clause that instructs the database how to join the two tables. The default is a Cartesian Product, i.e., joining every row from the first table with every row from the second table.
In this case, you can join on the userID:
SELECT u.*
FROM users u
JOIN player_league pl ON u.userID = pl.userID
WHERE pl.leagueID = 1
Probably you want something like this:
select * from user left join player_league on user.id = player_league.userId where player_league.leagueId = '1'
Hope this helps
Here is my query: I want to get the user values which is stored in user table and comments which is stored in comments table.
The user_id is the id of the user which i fetch from comments table.
How to get both comments with respective users how to do that with single query or there is any good and best way to do that?
mysql_query("select profile_pic,firstname,lastname,username,comment from user join comments where user_no IN (select user_id,comment from comments where question_id='".$_POST['get_comments']."') ");
Try
SELECT profile_pic,firstname,lastname,username,comment
FROM `user`
INNER JOIN comments ON `user`.user_no = comments.user_id
WHERE comments.question_id='".$_POST['get_comments']."'
i think the join condition you didn't specify and also you didn't specify what type of join you required try this code.
mysql_query("select profile_pic,firstname,lastname,username,comment from user left join comments on user.columnname=comments.columnname where user_no IN (select user_id,comment from comments where question_id='".$_POST['get_comments']."') ");
The column name specifies on which column you want to join to tables.for reference please read this