MySQL error: Operand should contain 1 column(s) - mysql

I have this MySQL Query (working)
First query:
SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (
SELECT *
FROM user_counter
WHERE users.id = user_counter.publisher_id
)
The MySQL query above is to find the user id from two table
Now I want to compared again using this second MySQL query (working)
Second query:
SELECT users.id, publisher_amount, publisher_now, publisher_max, counter
FROM users
INNER JOIN user_counter ON users.id = user_counter.publisher_id
WHERE no = 08123456789
AND counter < publisher_amount
But when I join all the query like this:
SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (
SELECT *
FROM user_counter
WHERE users.id = user_counter.publisher_id
)
AND (
SELECT users.id, publisher_amount, publisher_now, publisher_max, counter
FROM users
INNER JOIN user_counter ON users.id = user_counter.publisher_id
WHERE no =08123456789
AND counter < publisher_amount
)
I get this error:
Operand should contain 1 column(s)
Then, I try using 1 column, but the result is not what I wanted.
My question is How to join first and second query ? and produce no error.
I have tried google it and after many "try-and-error" this is the far I can get to make the query work.

I think you just miss an EXISTS on your second subquery. Anyway, if I understand your query correctly, I think you could write your query as this:
SELECT
u.id
FROM
users u inner join user_counter uc
on u.id=uc.publisher_id
and no=08123456789
and counter < publisher_amount
WHERE
u.publisher_set = '1'
AND u.publisher_status = '1'
AND u.publisher_content != ''
AND u.publisher_amount != '0'
AND u.publisher_now < publisher_max

You could change the first EXISTS clause to:
and users.id in (select user_counter.publisher_id from user_counter)
and use EXISTS on the final query.

You could also do this:
AND EXISTS (
SELECT user_counter.publisher_id
FROM user_counter
WHERE users.id = user_counter.publisher_id
PS: SQLFIDDLE doesn't work in my end for some reason. Else would have been happy to give you a demonstration ;)

SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (
select 1 from user_counter where users.id = user_counter.publisher_id
and no =08123456789
AND counter < publisher_amount
)
Assuming no and counter are on table user_counter. A bit hard to tell without a schema.

Related

How to fix join error in mysql update query

I had an error in mysql as picture. please help me
sql code is following:
UPDATE
tbl_users AS Users
SET
Users.money_current = Users.money_current +
CASE
WHEN TempTbl.money_info IS NULL
THEN 0
ELSE TempTbl.money_info
END
LEFT JOIN
(SELECT
userId,
SUM(bet_money * bet_rate) AS money_info
FROM
tbl_betting
WHERE ROUND = 'xxx'
AND is_win = 1
GROUP BY userId) AS TempTbl
ON Users.userId = TempTbl.userId
FROM tbl_users AS Users;
This is the correct syntax:
UPDATE tbl_users AS Users
LEFT JOIN (
SELECT userId, SUM(bet_money * bet_rate) AS money_info
FROM tbl_betting WHERE ROUND = '965802' AND is_win = 1
GROUP BY userId
) AS TempTbl ON Users.userId = TempTbl.userId
SET Users.money_current = Users.money_current + COALESCE(TempTbl.money_info, 0)
I also changed that CASE expression with COALESCE().
But I think an INNER JOIN would also work in your case, since the unmatched rows of the LEFT JOIN that you use do not change the value of money_current.

Get other different id

I have a simple mysql query:
SELECT U.id FROM
users U
INNER JOIN friends F
ON ( U.id = F.id_exp AND F.id_des = :id_exp )
OR ( U.id = F.id_des AND F.id_exp = :id_exp ) WHERE
U.id <> :id_des
AND F.active = 1
I wish that by calling this request again, that It does not return the id that It has already previously returned.
I hope you understand me.
Thank you.
You must give RAND a seed number in order to always get the same order. This can be any number, e.g. 1:
...
ORDER BY RAND(1)
LIMIT #offset, 1;

How to optimise query

This query returns all friends of logged in user, which is present in "network" table in below query. This maps frd_id and mem_id from these two tables.
I have optimized this query. but still it is taking time to get result.
Please suggest me optimized query for the same.
SELECT m.mem_id,m.profilenam,m.gender,m.photo_thumb,m.profile_type,n.frd_id as mem_id
FROM `network` n
left join members m on(n.frd_id=m.mem_id)
where ((n.frd_id = m.mem_id and n.mem_id=$uid)
or (n.mem_id = m.mem_id and n.frd_id=$uid))
AND m.ban='n'
AND m.deleted<>'Y'
and profilenam like '%'
Try this:
SELECT m.mem_id,m.profilenam,m.gender,m.photo_thumb,m.profile_type,m.mem_id
FROM `network` n
JOIN members m ON n.frd_id = m.mem_id
AND ( n.mem_id = $uid
OR ( n.mem_id = m.mem_id
AND n.frd_id = $uid
)
)
WHERE m.profilenam IS NOT NULL
AND m.ban = 'n'
AND m.deleted <> 'Y';

mysql not equal operator not working

When I try this query:
SELECT *
FROM sds_posts
WHERE topic_id = '2439'
AND author = ANY (SELECT mid
FROM sds_actions
WHERE whoami = '710' AND type = 'block')
AND status = '1'
AND deleted = '0'
ORDER BY
id ASC
LIMIT 50
it is working correctly.
But I need this one:
SELECT *
FROM sds_posts
WHERE topic_id = '2439'
AND author <> ANY (SELECT mid
FROM sds_actions
WHERE whoami = '710' AND type = 'block')
AND status = '1'
AND deleted = '0'
ORDER BY
id ASC
LIMIT 50
This time query have to select opposite of first query, but it is just select all author. I tried != and also NOT IN, but result is same.
So why? Why does <> not work as expected?
I would think that changing
and author = any...
to
and NOT author = any...
would work... But if that does not, then I would try doing as a left-join and looking for null. Since the author is the "mid" from the sds_actions, I would write it as...
SELECT
sp.*
FROM
sds_posts sp
LEFT JOIN sds_actions sa
on sp.author = sa.mid
AND sa.whoami = '710'
AND sa.type = 'block'
WHERE
sp.topic_id = '2439'
AND sp.status = '1'
AND sp.deleted = '0'
AND sa.mid IS NULL
ORDER by
sp.id ASC
LIMIT 50
You can try change author = any(...) to author IN (...)
and change author <> any(...) to author NOT IN (...)

how to select previous record with LEFT JOINed tables in MySQL?

I'm struggleing to get the following to work:
# prev
SELECT klha.buyerID < PARAMETER_1
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
ORDER BY klha.buyerID
LIMIT 1
;
END
The left joins are working as I'm using them in another procedure to select ALL relevent records. The problem is with my attempt to select the previous record.
I'm passing in a buyerID and need to get back the id of the previous record.
Question:
Can anyone point me to the correct syntax?
EDIT:
If I run this in MySQL, I'm getting "0" as resultset
Thanks!
SOLUTION:
Got it to work like this:
# prev
SELECT klha.buyerID
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
AND klha.buyerID < PARAMETER_1
ORDER BY klha.buyerID
LIMIT 1
;
Easier than thought :-) Thanks #Henrique Ordine
If I understand correctly what you need, adding this condition to your where clause should do the trick:
and klha.buyerID = (select max(buyerID) from agents where buyerID < PARAMETER_1)
Like this:
SELECT klha.buyerID
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
and klha.buyerID = (select max(buyerID) from agents
where buyerID < PARAMETER_1)
ORDER BY klha.buyerID
LIMIT 1