mySQL error need some help - mysql

I am trying to run a nested query but I am getting this error,
#1241 - Operand should contain 1 column(s)
this is the query that I am trying to run,
SELECT *
FROM `categoryTable`
WHERE `categoryId` NOT
IN (
SELECT `categoryTable`.`categoryId` , `categoryTable`.`categoryTitle` , `userMenuTable`.`menuEntryId`
FROM (
`categoryTable`
)
LEFT JOIN `userMenuTable` ON `categoryTable`.`categoryId` = `userMenuTable`.`categoryId`
WHERE `userMenuTable`.`cookieId` = 'bang4b696152b4869'
)
LIMIT 0 , 30

5th line should be
SELECT `categoryTable`.`categoryId`
i.e. it should only reference categoryId.
In other words, with the WHERE xyz [NOT] IN (SELECT ... predicate, there should only be one column in the nested select, one corresponding to the "xyz" column but of course not necessarily named the same. The reason is that SQL wouldn't know which column of the nested query to use for comparing with the "xyz" column; a lesser reason is that the other columns are useless, why bring them in?

Yes. I agree with #mjv, basically you are checking to see if categoryId is not in the list of
`SELECT `categoryTable`.`categoryId` , `categoryTable`.`categoryTitle` , `userMenuTable`.`menuEntryId`
FROM (
`categoryTable`
`
So you need to mention only one field categoryID and it will check to see if it is not in this list.
Hope this makes some sense.

Related

I have written query to sort name starting with S and V in give database , error coming up is below

select * from cricket_1111 , cricket_999
where first_name LIKE "s%v"
LIMIT 0, 1000
Error Code: 1052. Column 'first_name' in where clause is ambiguous 0.000 sec
You have a first_name column in both tables, so you have to specify which table you want to use like
select *
from cricket_1111 , cricket_999
where cricket_1111.first_name LIKE "s%v"
LIMIT 0, 1000
It would also be very productive if you took a look at the JOIN syntax rather than using from cricket_1111 , cricket_999
Something like
select *
from cricket_1111 c1
JOIN cricket_999 c9 on c9.link_column = c1.link_column
where c_1.first_name LIKE "s%v"
LIMIT 0, 1000
ORDER BY c_1.first_name
Also a limit clause without a sort ORDER BY can be very unpredictabe
Your error is due to the fact that the colum first_name appears in the two table. And the SQL engine cannot make an arbitrary choice ! You need to prefix the column name by the table name in the ORDER clause of the SELECT statement.

Need assistance with SQL Query with error message Operand should contain 1 column(s)

Good Day
I have the following query but I'm getting an error message 'Operand should contain 1 column(s)'
Any assistance would be greatly appreciated
UPDATE expenditure
SET BP = (
SELECT * ,
SUM(balance_provision - actual_amt_voucher) over (partition by voteid order by expenditureid) AS BalanceProvision
FROM expenditure
)
It looks like you want to update column bp with a window sum.
Your query fails because you are trying to assign a resultset (that has multiple columns) to a single column.
But even you were returning a scalar value from the subquery, this would not work, since MySQL does not allow reusing the target table of the update in a subquery.
Instead, yo can use the update ... join syntax. Assuming that expenditureid is the primary key of the table, as its name suggests, that would be:
update expenditure e
inner join (
select
expenditureid,
sum(balance_provision - actual_amt_voucher) over (partition by voteid order by expenditureid) bp
from expenditure
group by expenditureid
) e1 on e.expenditureid = e1.expenditureid
set e.bp = e1.bp

Getting an Operand should contain 1 column(s) error when merging values of two tables

I'll try to explain it the best I can. My English is not very good.
I want to combine values from two tables. It works fine when I only merge one table, like so:
SELECT `friendid`,`friendname`, (SELECT `islogged` FROM `account_data` WHERE `guildcard` = `friendid` ) FROM `guild_data` WHERE `accountid` = '42000007' ORDER BY `friendid` DESC LIMIT 0, 40
But I need two entries from the "account_data" table. I thought I could do it like this:
SELECT `friendid`,`friendname`, (SELECT `islogged`,`lastonline` FROM `account_data` WHERE `guildcard` = `friendid` ) FROM `guild_data` WHERE `accountid` = '42000007' ORDER BY `friendid` DESC LIMIT 0, 40
But apparently that isn't correct, and it throws me an error, namely:
[Err] 1241 - Operand should contain 1 column(s)
How can I make this work with 2 columns? Both values should be taken from the row with the guildcard value found from friendid.
Hope I've been clear enough.
It looks like you need to use a JOIN. Try this:
SELECT g.friendid, g.friendname, a.islogged, a.lastonline
FROM guild_data g
LEFT OUTER JOIN account_data a
ON g.friendid = a.guildcard
WHERE g.accountid = '42000007'
ORDER BY g.friendid DESC LIMIT 0, 40
Using a LEFT OUTER JOIN will ensure that you get results in guild_data that don't have rows in account_data. If you know that you will always have a corresponding row in account_data or if you want to exclude rows in guild_data that don't map to anything in account_data, use INNER JOIN instead.
If this doesn't give you the results you want, please let us know what is wrong with it. Post what you got with this query and what you expected to get.

SQL Union All alias from first table?

I have two tables
[data] - title,maker,partnum,price
[cross] - product(data.partnum),title,maker,partnum,price
What I want is listing all product via sysn number. How can I get with union all data like that with ordering ->
[data table] Microsoft, "some note", 9989, $20
[cross table] Microsoft reseller, "some note", 1045, $30
[cross table] Apple reseller in Microsoft :), "some note", 2233, $40
virtual spacer :)
[data table] Microsoft, "some note", 9989, $10
[cross table] Lenovo reseller in Microsoft..
Im trying with this
SELECT `title`,'Microsoft' AS `maker`,`partnum`,`price`
FROM data as d
WHERE sysn=%s
GROUP BY partnum
UNION ALL
SELECT `title`,`maker`,`partnum`,`price`
FROM cross as c
WHERE c.product=d.partnum
GROUP BY `partnum`
Thanks
It's not entirely clear what you are asking. Consider setting up an example schema and data in http://sqlfiddle.com.
A "virtual spacer" is going to be very messy to achieve. It's not totally impossible, just messy.
For returning rows in a particular sequence, you are going to need to add an ORDER BY clause.
It's not at all clear why you are including GROUP BY clauses. The GROUP BY on the first SELECT would effectively "collapse" example output rows 1. and 5. given the same value 9989 for partnum. Absent an explanation of why you need a GROUP BY, I'm going to guess that what you really intended was an ORDER BY clause.
It's also not clear why the literal value 'Microsoft' would need to appear in the SELECT list, in place of the maker column from the data table. There's nothing invalid SQL-wise with doing that. But absent an explanation, it just doesn't make much sense.
The question we should to ask... Why is this specific result needed? Is there a different result which would be easier to achieve, which would equally satisfy the requirements?
Setting aside the "virtual spacer" row, the return from a query like this seems like it satisfies most of the specification:
SELECT t.src
, t.title
, t.maker
, t.partnum
, t.price
FROM (
SELECT '[data]' AS `src`
, d1.title AS `title`
, d1.maker AS `maker`
, d1.partnum AS `partnum`
, d1.price AS `price`
, d1.partnum AS `product`
FROM data d1
WHERE d1.sysn = ?
UNION ALL
SELECT '[cross]'
, c2.title
, c2.maker
, c2.partnum
, c2.price
, c2.product
FROM cross c2
JOIN data d2
ON d2.partnum = c2.product
WHERE d2.sysn = ?
)
ORDER BY t.product DESC, t.src DESC, t.price ASC
If you are using MySQL, "cross" is a reserved word and you must enclose cross in backticks to use it as a table name (The backtick is next the the '1' on the keyboard on the upper left).

Select data from another table with where clause

What's wrong with this query, im selecting data from 3 different tables here. First title of exam from "class_exams" table , second selecting sum of total marks from "results" table. Query works fine without where clause.
SELECT id, exam_date , (
SELECT title
FROM class_exams
WHERE result_heads.exam_id = class_exams.id
) AS exam_title, (
SELECT sum( marks )
FROM results
WHERE result_heads.id = results.head_id
) AS obt_marks
FROM `result_heads` WHERE exam_title = 'test';
Error comes
Unknown column 'exam_title' in 'where clause'
Consider using Join
If I understand the table schema , it should be like this :
SELECT result_heads.id, result_heads.exam_date , sum( results.marks )AS obt_marks
FROM results JOIN result_heads
ON results.exam_id = result_heads.id
GROUP BY result_heads.id, result_heads.exam_date
I think you need to add the name of table in where clouse
WHERE `tbl_name`.`exam_title = 'test';
I know this is an old post, but I like to fill the answers where old posts end to prevent dead end posting. It looks like the table name is not being called out in the From clause
See this link http://www.techonthenet.com/mysql/where.php and look at the example - Joining Tables.