I have two database tables, one is named "articles", and the other one is "users"
the structure of articles is as follows:
article_id article_title user_id
The structure of users is:
user_id user_fullname password
I wish to retrieve a list of all articles from the table "articles", but would like to attach each article's user_fullname. I think this may require "LEFT JOIN", so I made the following attempt in MySQL prompt.
> SELECT * FROM articles A LEFT JOIN users U on U.user_id = A.user_id;
but somehow I don't see the user_fullname printed out with this command. I need some help with the correct syntax. Thank you!
A full join seems more appropriate, each article must have an author, so there should be a corresponding entry in the users table:
SELECT *
FROM articles a
JOIN users u USING(user_id);
Note: USING here is the same as ON a.user_id = b.user_id and can only be used if the column name is the same in both tables.
Related
Ill make it simple I have 2 tables in my database
1. Post
2. User
The columns inside the Post table are
Post ID
User ID
Post Title
Post Content
Then the columns in User table are
User ID - Connected to Post.User ID
User Full Name
Then I want to do now is to select the post posted by Juan Cruz(User.User Full Name). Help Please, I cannot find these question anywhere.
PS Sorry for my bad english.
EDIT: I will use this as my search module on my php/html project. So I need to know the post/s posted by Juan Cruz insted of User ID of Juan Cruz. Thanks Again
Hope this might help:
SELECT
p.post_id, p.user_id, p.post_title, p.post_content,
u.user_fullname
// column name from two or more tables
// eg: table_name.column_name
// use p and u, because we already give temporary name for table post and user
FROM
post p // table 1: post refer as p (SQL aliases)
JOIN // combine 2 column from two or more tables
user u // table 2: user refer as u (SQL aliases)
ON
p.user_id = u.user_id // key for joining the table, the ID should be the same
or you can use below code without aliases :
SELECT
post.post_id, post.user_id, post.post_title, post.post_content,
user.user_fullname
FROM
post
JOIN
user
ON
post.user_id = user.user_id
Refer: SQL JOIN
You need to use a JOIN. This should be updated to use your column names.
SELECT p.id, p.title, p.content, u.full_name FROM User AS u JOIN Post AS p ON (u.id = p.user_id);
I'm looking for a way to select users from a user table that have at least one relating entry in another table. Probably my join approach is totally wrong but that is what I was trying and should give you an idea of the structure:
SELECT users.`id`, users.`email`, users.`username`, users.`status`, users.`date_reg`
FROM dir_editors as users
JOIN dir_links as links ON (links.`id_editor` = users.`id`)
WHERE COUNT(links.`id_editor`) > 1
So the goal is to get all these user data from user that have at least one link entry in the dir_links table where the id_editor is the field in the dir_links table.
Thanks for helping
SELECT users.`id`, users.`email`, users.`username`, users.`status`, users.`date_reg`
FROM dir_editors as users
WHERE EXISTS(SELECT 1 FROM dir_links as links WHERE links.`id_editor` = users.`id`)
SELECT users.`id`, users.`email`, users.`username`, users.`status`, users.`date_reg`
FROM dir_editors as users
JOIN dir_links as links ON (links.`id_editor` = users.`id`)
Remove this WHERE COUNT(links.id_editor) > 1 because this imposing condition.
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
An INNER JOIN selects the records that are found in both tables, depending on a key that you put in your ON statement:
SELECT users.id, users.email, users.username, users.status, users.date_reg
FROM dir_editors AS users
INNER JOIN dir_links as links
ON links.id_editor = users.id
I have a users table and an edits table (showing who performed changes on their own or someone else's profile).
In the edits table, the editor and editee are listed using their userid, which is the unique id in the users table.
I would like to create the query:
Select users.username (the editee), users.username (the editor) from users
inner join edits on users.id = edits.editee_id
How would I create a subquery to pull the editor's name?
Thanks
You need to join the users table twice.
SELECT whatever,
editor.username AS editor_username,
editee.username AS editee_username
FROM edits
JOIN users AS editor ON edits.editor_id = editor.id
JOIN users AS editee ON edits.editee_id = editee.id
See what's going on? You use the users table twice, and give it a different alias in each use.
I'm probably being a bit dumb, hopefully someone can help.
I have a simple 2 column user table (ID, USERNAME).
I have a comments table for images (COMMENT, COMMENTFROM, COMMENTTO)
COMMENTFROM is the ID of the user who made the comment. COMMENTTO is the ID of the owner of the image that the comment was added to. Both users are held within the USERS table.
I want to pull out and display rows like this
"really nice photo" - to USERXYZ - from USER123**
This has puzzled me, because if I join the USERS table to the comments table on:
WHERE comments.userfrom = users.id
That only gets me one (or the other) of the 2 usernames I need per row. Is there a way I can get both?
I'm not even sure how I would search for this answer on SOF, apologies if it has been answered before. If anyone can point me in the right direction it would be appreciated :)
You need to JOIN to the users table twice, and give them different identifiers (aka aliases) on each JOIN within your SQL.
SELECT
comment,
userFrom.username AS commentFrom,
userTo.username AS commentTo
FROM comments
JOIN users AS userFrom ON userFrom.ID = comment.commentFrom
JOIN users AS userTo ON userTo.ID = comment.commentTo
Please try following one
SELECT CONCAT(c.comment," - to ",
(SELECT USERNAME FROM user WHERE user.ID = c.COMMENTTO LIMIT 1),
" - from ",
(SELECT USERNAME FROM user WHERE user.ID = c.COMMENTFROM LIMIT 1)) FROM comments c
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