#1241 - Operand should contain 1 column(s) MYSQL [duplicate] - mysql

This question already has answers here:
MySQL - Operand should contain 1 column(s)
(10 answers)
Closed 8 years ago.
I am trying to get data from the database like that, but i have this error how can i fix?
SELECT post.text,users.name,users.surname,users.profile_id,post.post_id,comments.text as comment,
(SELECT user.name, user.surname FROM users user WHERE profile_id = comments.profile_id) as name_comment
FROM post
INNER JOIN users ON users.profile_id = post.profile_id
INNER JOIN comments ON comments.profile_post = post.post_id

Simply JOIN to the users table twice
SELECT
post.text,
userpost.name,
userpost.surname,
userpost.profile_id,
post.post_id,
comments.text as comment,
usercomment.name, usercomment.surname -- this
FROM post
INNER JOIN users userpost ON userpost.profile_id = post.profile_id
INNER JOIN comments ON comments.profile_post = post.post_id
INNER JOIN users usercomment ON comments.profile_id = usercomment.profile_id

try this
SELECT post.text,users.name,users.surname,users.profile_id,post.post_id,comments.text as comment
FROM post
INNER JOIN users ON users.profile_id = post.profile_id
INNER JOIN comments ON comments.profile_post = post.post_id
WHERE profile_id = comments.profile_id

Your subquery:
(SELECT user.name, user.surname
FROM users user
WHERE profile_id = comments.profile_id) as name_comment
has 2 fields instead one
You can:
Use 2 distinct subquery to get user.name and user.surname;
concatenate the two information so you have one output field;
Why you use subquery when you have joined your users table in the main query (with the same condition)

Related

MySQL join a table to another table only if [duplicate]

This question already has answers here:
conditional join in mysql
(4 answers)
Closed 6 years ago.
I have a little complicated query here. I have to select columns by names cant just user 'orders.*. because i have multiple columns from different tables with the same name.
What I'm trying to do is to select specific fields from orders,users,payment_methods_translation and join bank_accounts_translation only if the orders.payment_method_id = '3'
SELECT
orders.id as orderid,
orders.final_total,
orders.user_id,
orders.auto_cancel,
users.id as userid,
users.first_name,
payment_methods_translation.payment_method_id,
payment_methods_translation.name
FROM
orders,users,
payment_methods_translation
WHERE
orders.id='$id' AND
orders.user_id = users.id AND
orders.payment_method_id = payment_methods_translation.payment_method_id AND
orders.auto_cancel='1'
JOIN
bank_accounts_translation ON (orders.payment_method_id='3'
AND orders.bank_id = bank_accounts_translation.bank_account_id)
But I get a mysql error. So how can I select all the fields from bank_accounts_translation only if orders.payment_method_id = '3' and orders.bank_id = bank_accounts_translation.bank_id
Left join should do it... I
Refactored the code to use the current ANSI standards, you appear to be mixing them.
used table aliases to improve readability.
LEFT JOIN says return all records from prior listed tables and only those that match the on criteria of the table being joined to.
SELECT O.id as orderid
,O.final_total
,O.user_id
,O.auto_cancel
,u.id as userid
,u.first_name
,PMT.payment_method_id
,PMT.name
FROM orders O
INNER JOIN users U
ON O.user_id = U.id
INNER JOIN payment_methods_translation PMT
ON O.payment_method_id = PMT.payment_method_id
LEFT JOIN bank_accounts_translation BAT
ON O.payment_method_id='3'
and O.bank_id = BAT.bank_account_id
WHERE O.id='$id'
and O.auto_cancel='1'

Getting No Output When INNER JOIN Is Used Twice In A Query

I am new to SQL Join topic. I don't know why my SQL Query is not working properly. Here is the query:
SELECT * from post
INNER JOIN user ON post.id = user.id
INNER JOIN follower ON user.id= follower.id
WHERE follower.fid = 20 OR user.id < 1000
ORDER BY pid DESC LIMIT 7
If I use this query it works:
SELECT * from post
INNER JOIN user ON post.id = user.id
WHERE user.id < 1000
ORDER BY pid DESC LIMIT 7
But adding another INNER JOIN gave no output.
Update :
I am using 2 INNER JOINS so that I can show data from 3 table if there are no followers in follower table then show data from 2 tables(post and user) only where id in user table is less then 1000.
Its because you probably have no follower on the post:
SELECT * from post
INNER JOIN user ON post.id = user.id
LEFT OUTER JOIN follower ON user.id= follower.id -- changed JOIN type
WHERE follower.fid = 20 OR user.id < 1000
ORDER BY pid DESC LIMIT 7
Remember INNER JOIN only returns if it finds the match in all JOIN conditions. Any condition will fail and you won't get the row.
For example:
You have a post, but the user has no follower. INNER JOIN will skip the row. Here comes the OUTER JOIN! it will give you the user row even when no follower is found.

MySql how to combine LEFT JOIN query with NOT IN query

I need to combine LEFT JOIN query with NOT IN query and have to get the result values from these queries. I have get the correct results while using these two queries at separately.
I have two tables likely user and answer
user table
user_id
1
2
3
4
5
answer table
user_id date
1 2015-10-15 21:23:14
2 2015-10-15 20:23:14
3 2015-11-11 16:23:14
LEFT JOIN query:
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE DATEDIFF(NOW(),answer.date) > 5
This query returns result user_id 1,2.
NOT IN query:
SELECT user.user_id
FROM user
WHERE user.user_id NOT IN (SELECT answer.user_id FROM answer)
This query returns result user_ids 4, 5.
I need to combine this two queries into single query so I tried with these two below queries:
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE (DATEDIFF(NOW(),answer.date) > 5
AND user.user_id NOT IN (SELECT answer.user_id FROM answer))
and
SELECT user.user_id
FROM user
LEFT JOIN answer
ON user.user_id = answer.user_id
WHERE user.user_id NOT IN (SELECT answer.user_id FROM answer)
AND DATEDIFF(NOW(),answer.date) > 5
But these return empty user_id.
Edit
Expected result should contain values of 1,2,4,5
If your meaning is about to combine 2 selection together Try Union All / Union :
SELECT * FROM
(
SELECT [WITH YOUR LEFT JOIN STATEMENT]
UNION
SELECT [WITH YOUR NOT IN STATEMENT]
) ResultTABLE
If you refer to the answer table in the WHERE clause, the answer.date will be restricted to non-NULL value, making the LEFT JOIN behave like a regular join. Move the condition to the ON condition to retrieve all the users, plus maybe the matching rows in answers:
SELECT user.user_id
FROM user u
LEFT JOIN answer a ON u.user_id = a.user_id
AND DATEDIFF(NOW(),a.date) < 5
;
Edit: after the edit of the question it appears the OP wants joined records with non existing answer OR answer.date too old/young:
SELECT user.user_id
FROM user u
LEFT JOIN answer a ON u.user_id = a.user_id
WHERE a.date IS NULL -- no answer record
OR DATEDIFF(NOW(),a.date) > 5 -- too old answer record
;
Final version: Since the OP wants to find users who don't have a (recent) answer, the query can be simplified to:
SELECT user.user_id
FROM user u
WHERE NOT EXISTS (
SELECT * FROM answer a
WHERE a.user_id = u.user_id
AND DATEDIFF(NOW(), a.date) <= 5
);

Mysql full outer join 3 tables with group by and count

My query is only showing a subset of records of people who have messages for me whereas I want to return a list of all users wether they have messages for me or not along with the count (or true/false, 0,1)
SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to)
from attendee_chat
INNER JOIN attendee ON attendee.id = attendee_chat.to
INNER JOIN chat ON attendee_chat.id = chat.attendee_chat_id
WHERE attendee.id <> 1
GROUP BY attendee_chat.to;
picture is worth a thousand words.
Put the attendee table first in the FROM, and then left join to the other tables that may not have records.
SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to)
from attendee
LEFT JOIN attendee_chat ON attendee_chat.to = attendee.id
LEFT JOIN chat ON attendee_chat.id = chat.attendee_chat_id
WHERE attendee.id <> 1
GROUP BY attendee_chat.to;
I would avoid doing a FULL OUTER JOIN as it is not necessary and may lead to some unexpected results.
EDIT: Try this. Remove the chat table as you aren't selecting from it anyway, and group on the fields from the primary table attendee, rather than the on tables that may not contain any data.
SELECT attendee.id, attendee.firstName, attendee.lastName, count(attendee_chat.to)
from attendee
LEFT JOIN attendee_chat ON attendee_chat.to = attendee.id
WHERE attendee.id <> 1
GROUP BY attendee.id, attendee.firstName, attendee.lastName;
EDIT2: After discussion of requirements that he wants a count of chats that were to him (#1)
SELECT attendee.id, attendee.firstName, attendee.lastName,
(SELECT count(1) FROM attendee_chat
WHERE attendee_chat.from = attendee.id and attendee_chat.to = 1) as chatcount
from attendee ;
EDIT3: After comment about external ID
SELECT attendee.id, attendee.firstName, attendee.lastName, attendee.attendee_id,
(SELECT count(1) FROM attendee_chat
INNER JOIN attendee a2 ON a2.id = attendee_chat.toid
WHERE attendee_chat.fromid = attendee.id and a2.attendee_id = 123
) as chatcount
FROM attendee ;
P.S. I've changed from to fromid and to -> toid as it is dangerous using reserved words as object names ;-) SQL Fiddle here
If you want to do a join on records even if they have no matching element(s) in the other relation, you probably want an OUTER JOIN instead of an INNER JOIN.
MySQL: Quick breakdown of the types of joins

Single query to get the messages and count their answers

I have a mysql DB with two tables:
messages
message_answers
I'd like to fetch all messages and the number of answers for each of them, like:
first message (10 answers)
second message (5 answers)
Is it possible with a single sql query ?
I tried a query and a subquery for the count, but I don't know how to have the current id from the main query to make the "WHERE" restriction on the subquery (like: "message_answers.message_id = messages.id").
Thank you,
Sébastien
SELECT m.message, COUNT(ma.answer_id) AS AnswerCount
FROM messages m
LEFT JOIN message_answers ma
ON m.id = ma.message_id
GROUP BY m.message
select m.id, count(ma.id)
from messages m
join join message_answer ma on ma.id = m.message_id
group by 1