How to get 2 column values from single subquery - mysql

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

Related

SQL JOIN cant get it working

I know the forum is full of this questions but i cant find the solution.
I want to join the table users from user_to_designment they both have a column with user_id the error that i get is:
select user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
LIMIT 0, 25
MySQL meldt: Documentatie
#1052 - Column 'user_id' in field list is ambiguous
I use this query:
select user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
Please some advice
In your select list prefix the user_id with the table name:
select users.user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
Both columns have user_id, SQL cannot choose between them, you must specify explicitly.
You need to specify which user_id to return in your select, eg select users.user_id or Select user_to_designment.user_id
You must clarify which user_id table column you are selecting.
select a.user_id,b.designment_id from user_to_designment b
full join users a
on b.user_id=a.user_id
This is because "user_id" field exists on both tables.
you must put table name before field name.
select user_to_designment.user_id, designment_id FROM user_to_designment
FULL JOIN users
ON user_to_designment.user_id = users.user_id
It seems user_id exist in both the tables. so it gives the ambiguous error. We should say from which table we have to pick the column.
SELECT users.user_id
,user_to_designment.designment_id
FROM user_to_designment
FULL JOIN users ON user_to_designment.user_id = users.user_id
You may use either users.user_id or user_to_designment.user_id in your select statement. Always use tablename.columnname format. It avoids confusion.

Find and replace table data with another table in MySQL

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...

mysql different select queries in one

how can I have with one query the following:
I would like to have from my comments table all the people how have been commenting on a given post_id and than check how many time the user has commented, based on his name. I would like to avoid to have 2 different queries for it
I have been trying the following but won't return to expected result
SELECT comments.*, COUNT(approved.comment_approved) AS has_commented FROM wp_comments AS comments
INNER JOIN wp_comments AS approved
ON comments.comment_author = approved.comment_author
WHERE comments.comment_post_ID =14616
GROUP BY comments.comment_content
Shouldn't you group by post_ID ? (that would return only one line)
SELECT
comments.*
, COUNT(approved.comment_approved) AS "has_commented"
FROM wp_comments AS comments
JOIN wp_comments AS approved
ON (comments.comment_author = approved.comment_author)
WHERE comments.comment_post_ID = 14616
GROUP BY comments.comment_post_ID
;
Or do you want one line per "approved" comment ?

MySQL select rows that do not have matching column in other table

I can't seem to figure this out so far. I am trying to join two tables and only select the rows in table A that do not have a matching column in table B. For example, lets assume we have a users table and a sent table.
users table has the following columns: id, username
sent table has the following columns: id, username
I want to select all rows from users where username does not exist in sent table. So, if tom is in users and in sent he will not be selected. If he is in users but not in sent he will be selected. I tried this but it didn't work at all:
SELECT pooltest.name,senttest.sentname
FROM pooltest,senttest
WHERE pooltest.name != senttest.sentname
Typically, you would use NOT EXISTS for this type of query
SELECT p.Name
FROM pooltest p
WHERE NOT EXISTS (SELECT s.Name
FROM senttest s
WHERE s.Name = p.Name)
An alternative would be to use a LEFT OUTER JOIN and check for NULL
SELECT p.Name
FROM pooltest p
LEFT OUTER JOIN senttest s ON s.Name = p.Name
WHERE s.Name IS NULL
Note that the implicit join syntax you are using is considered obsolete and should be replaced with an explicit join.
Try this SQL:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.username = users.username
WHERE sent.username IS NULL;
The better way in my opinion would be:
SELECT users.username
FROM users
LEFT JOIN sent ON sent.id = users.id
WHERE sent.id IS NULL;
As both the id fields, would be indexed (primary key I would have thought) so this query would be better optimised than the first one I suggested.
However you may find my first suggestion better for you, it depends on what your requirements are for your application.
May be this one can help you ....
I had also the same problem but Solved using this this query
INSERT INTO tbl1 (id,name) SELECT id,name from tbl2 where (name) not in(select name from tbl1);
hope this one will solve your problem

Mysql group concat on double join

I have a user table from which I want all values, so I have this query:
SELECT tbl_user.* FROM tbl_user
Now I want one additional column in this result which shows all roles this user has, (or nothing if there are no roles for the user). The role information comes from two additional tables.
The first table contains these two values: userid, roleid
The second table contains roleid and role_name.
So the group concat needs to get all role names based on the roleid's in table1.
I have tried several different ways to do this, but I don't succeed. Either I get only one result with several times the same rolename, or no result at all.
Thanks for your help
Michael
Update: added LEFT JOIN for users with no role.
SELECT
tbl_user.*,
GROUP_CONCAT(role_name) AS roles
FROM
tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.userid
JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
GROUP BY tbl_user.userid
Note that MySQL will permit a GROUP BY on fewer columns than appear in the SELECT list in total, but in other RDBMS you would need to explicitly list out the columns in tbl_user and include them in the GROUP BY, or do an additional self join against tbl_user to get the remaining columns from that table.
Something like:
SELECT
urole.userid,
uall.username,
uall.name,
uall.othercols,
urole.roles
FROM
tbl_user uall JOIN (
SELECT
tbl_user.userid,
GROUP_CONCAT(role_name) AS roles
FROM
tbl_user LEFT JOIN tbl_roles ON tbl_user.userid = tbl_roles.roleid
JOIN tbl_rolenames ON tbl_roles.roleid = tbl_rolenames.roleid
GROUP BY tbl_user.userid
) urole ON uall.userid = urole.userid