Unknown column '2' in 'order clause' - mysql

select count(distinct(vw_SIPMIP.product_id)) from vw_SIPMIP , sp_mip_rule
where
vw_SIPMIP.product_id not in (select a.product_id from vw_non_SIPMIP a)
and sp_mip_rule.id = vw_SIPMIP.id
and sp_mip_rule.createdby != '_IMPORT' limit 1
I am keep getting this error

your syntax is incorrect on the distinct... By using the (parens), it is thinking Distinct is a function and expecting the inner value as a parameter to pass and get a value back... what you want is... Additionally, since you have no other columns you are returning, you don't need a limit one... COUNT(*) or COUNT(DISTINCT SomeColumn) will ALWAYS return a single row all by itself... no group by needed.
select count(distinct vw_SIPMIP.product_id) YourDistinctCount
from vw_SIPMIP,
sp_mip_rule
where vw_SIPMIP.product_id not in (select a.product_id from vw_non_SIPMIP a)
and sp_mip_rule.id = vw_SIPMIP.id
and sp_mip_rule.createdby != '_IMPORT'

Related

mySQL 'where name NOT IN' not working?

I have this statement:
SELECT id, name
FROM players
WHERE this = 1
AND name NOT IN (SELECT name
FROM players_online
WHERE this = 'that')
ORDER BY RAND()
LIMIT 3, 6
All columns are correctly named. No SQL errors occur, it just returns no results.
From what I've read in documentation it should work, but doesn't.
Any idea?
Don't use NOT IN with subqueries. It has the wrong semantics if any value in the subquery is NULL. In that case, the NOT IN never evaluates to TRUE, so no rows are returned at all.
Instead, use NOT EXISTS:
SELECT p.id, p.name
FROM players p
WHERE p.this = 1 AND
NOT EXISTS (SELECT 1
FROM players_online po
WHERE po.name = p.name AND po.this = 'that'
)
ORDER BY RAND()
LIMIT 3, 6;

SQL Server return NULL (or value) in case entry does not exist - Multiple Columns

I have a populated table with a 'Number' as PK. I would like a query which searches for a specific number, and if it is not found then it would return "NULL" not a no value.
I have managed to do it for one return:
SELECT (SELECT Risk_Impact FROM [dbo].[RFC] WHERE Number = 'RFC-018345')
However I would like to select multiple columns like:
SELECT (SELECT Risk_Impact, Risk_Impact, BI_TestingOutcome FROM [dbo].[RFC] WHERE Number = 'RFC-018345')
However it is giving me an error:
"Msg 116, Level 16, State 1, Line 1
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
Can you please assist?
Thank you in advance
Try
select p.* from (select 1 as t ) v
left join (select * from [dbo].[RFC] WHERE Number = 'RFC-018345') p
on 1=1
In sql server should be use isnull function like this
SELECT isnull((SELECT Risk_Impact FROM [dbo].[RFC] WHERE Number = 'RFC-018345'),0)
Refined #Sagar query:
And better indentation would probably be more helpful for people to learn from.
Ex:
SELECT
[dbo].[RFC].*
FROM
(SELECT 1 AS Col1) AS V
CROSS JOIN
[dbo].[RFC]
WHERE Number = 'RFC-018345'
Edit: I got caught up in removing the subquery. Working query:
SELECT
[dbo].[RFC].*
FROM
(SELECT 1 AS Col1) AS V
LEFT JOIN
[dbo].[RFC]
ON Number = 'RFC-018345'
(I would also advocate to not SELECT *, anywhere near production, but to always be explicit about the columns in the result set. Ex:
SELECT
[dbo].[RFC].[Risk_Impact],
[dbo].[RFC].[BI_TestingOutcome]
FROM
(SELECT 1 AS Col1) AS V
LEFT JOIN
[dbo].[RFC]
ON Number = 'RFC-018345'
)

Mysql with in Query -Operand contains one column

I have this query, it is giving me error
Operand Should contains one column. But i need two columns for now . Can anybody help me out
SELECT `business`.`id` AS business_id,
`business`.`name` AS business_name,
`business`.`address`,
`business`.`address2`,
`business`.`city`,
`business`.`state`,
`business`.`zipcode`,
`business`.`lat`,
`business`.`lon`,
`service`.`id` AS ser_id,
`service`.`name` AS service_name,
(SELECT price, `price_verified`.`comments`
FROM `price_verified`
LEFT JOIN `price`
ON `price_verified`.`price_id` = `price`.`id`
WHERE `price`.`service_id` = ser_id
AND `price_verified`.`status` = 1
ORDER BY `price_verified`.`date_verified` DESC
LIMIT 1)
FROM service
LEFT JOIN `business` ON `service`.`business_id` = `business`.`id`
WHERE `service`.`business_id` = 1
Thanks
AS the error message says you can only have one result column in your sub-query.
either you have to concatenate the sub query result or have two sub queries for each column.
like:
(SELECT concat(price,`price_verified`.`comments`) as result
FROM `price_verified` LEFT JOIN `price` ON `price_verified`.`price_id`= `price`.`id` \n\
WHERE `price`.`service_id`=ser_id AND `price_verified`.`status`= 1 ORDER BY `price_verified`.`date_verified` DESC LIMIT 1)

MySQL unknown column using subquery

I receive an error when i execute this query:
SELECT
(SELECT count(cp_projeto_view.id) FROM cp_projeto_view WHERE cp_projeto_view.id_projeto = cp_projeto.id AND cp_projeto_view.id_pessoa = 467 LIMIT 1) AS qtde_visualizacoes
FROM cp_projeto
WHERE qtde_visualizacoes = 0
The error is: #1054 - Unknown column 'qtde_visualizacoes' in 'where clause'
Why qtde_visualizacoes does not exists?
Thank you very much!
You cannot use a column alias in a where clause (unless you use a subquery). MySQL has an extension where you can use having instead:
SELECT (SELECT count(cp_projeto_view.id)
FROM cp_projeto_view
WHERE cp_projeto_view.id_projeto = cp_projeto.id AND cp_projeto_view.id_pessoa = 467
LIMIT 1
) AS qtde_visualizacoes
FROM cp_projeto
HAVING qtde_visualizacoes = 0
EDIT:
The query that you probably want is more like:
select p.*
from cp_projecto p
where not exists (select 1 from cp_projeto_view pv where pv.id_projeto = p.id and pv.id_pessoa = 467)
This will return all cp_projectos that have no matching rows in cp_projeto_view. Your original query would only return rows with a single column of 0s, which doesn't make much sense. If you want the count, then do:
select count(*) as cnt
from cp_projecto p
where not exists (select 1 from cp_projeto_view pv where pv.id_projeto = p.id and pv.id_pessoa = 467)
And, for performance, create an index on cp_projeto_view(projeto, id_pessoa).

"Subquery returned more than 1 value" with UPDATE query

Hello I write this sql query to update a table. The query is:
Update Product Set MakeId = (Select Distinct v.Id from VehicleCompany v, Car_Window c, product p Where p.ProductSKU = c.mf_part_no
AND c.make = v.Name
Group By v.Id)
But whenever I run this query it gives the following error:
Msg 512, Level 16, State 1, Line 1 Subquery returned more than 1
value. This is not permitted when the subquery follows =, !=, <, <= ,
, >= or when the subquery is used as an expression.
May I know Where I'm making the mistake?
Thanks in Advance
Your Select Distinct can return multiple values. To assign a value to MakeId your subquery must return only 1 value.
I am not sure whether that's what you want. You could write (Select Top 1 ...)
Run
Select Distinct v.Id
from VehicleCompany v, Car_Window c, product p
Where p.ProductSKU = c.mf_part_no
AND c.make = v.Name
You should see it is bring back more than 1 row and you will need to fix this part of the query
If you are confident the ID will always be the same changing to this will fix it for you:
Select top 1 Distinct v.Id from VehicleCompany v, Car_Window c, product p
Where p.ProductSKU = c.mf_part_no
AND c.make = v.Name