SQl Query to retrieve data - mysql

I cannot figure out how to get the required data from my table. The query which I wrote shows an error saying subquery returns more than one row..
SELECT name
FROM `business`
WHERE id = (
SELECT business_id
FROM bill
WHERE id = (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )
Here the subquery for bill_schedule returns more than one row, where show_bill is a boolean column. All I want here is to display the 'name' from the business whose show_bill is set to 1.

SELECT `name`
FROM `business`
WHERE id in (
SELECT business_id
FROM bill
WHERE id in (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )

Since the sub query is returning multiple rows, you can't use an equality operator
Just change the = to IN in your where clause:
SELECT name
FROM `business`
WHERE id IN (
SELECT business_id
FROM bill
WHERE id IN (
SELECT bill_id
FROM bill_schedule
WHERE show_bill = 1 )

Related

SQL : Find if table's 'A' ID exists to table's 'B' ID

I have 2 tables. Table A is priceList that contains 2 columns. ID and currency_id . Table B is priceListItems that contains 3 columns. Product priceListID ProductID.
For Example priceList
ID
CurrencyID
3
DF10CCE
And priceListItems
ID
priceListID
Product
1
3
DESK
I would like to write a statement in SQL to return a boolean (0 or 1) if the priceList has Items in, comparing with priceListItems based on their ID columns (For table A: ID , and for Table B: priceListID )
So if the priceList's ID exists in priceListItems's priceListID, the result should be true.
How i can achieve that?
Is the follwing correct?
SELECT priceList.id,
IF(priceListItems.id IS NULL, FALSE, TRUE) as priceListItems
FROM priceList
LEFT JOIN priceListItems ON (priceList.id = priceListItems.id)
SELECT id,
EXISTS ( SELECT NULL
FROM priceListItems
WHERE priceList.id = priceListItems.priceListID )
FROM priceList;
priceList.id = priceListItems.id this test is incorrect and should be priceList.id = priceListItems.pricelistid
and IF(priceListItems.id IS NULL, FALSE, TRUE) is dubious surely if null then true makes more sense..
The if() function can get the job nicely done. Assuming ID 4 from the pricelist has two rows in the pricelistitems and ID 5 from the pricelist has no entry in the pricelistitems, we can try:
insert priceList values(3,'DF10CCE'),(4,'DF11223'),(5,'DD11225');
insert priceListItems values(1,3,'desk'),(2,4,'pp'),(3,4,'ss');
select id, if((select count(*) from priceListItems where pricelistid=pl.id),1,0) from priceList pl;
The correlated subquery (select count(*) from priceListItems where pricelistid=pl.id) returns an int which is greater or equal to 0, which can be used directly as a boolean in the if() function to determine which value should be returned.

Find the same list ID of four tuples in a stored procedure

I'm writing a procedure...
I have 4 parameters in this procedure.
I want to find the same list ID from a Table using this four parameters.
With other words, if all IDs have same List_ID then return List_ID, if
not return NULL, and admitting that some IDs might be NULL, and some
IDs repeats in that table, so it should not fail if ID is null or
repeats
The difficult part is that not all the times I have that four IDs set , and I might have only two IDs set and other two set as NULL.
For example:
Table A
------------------------
ID List_ID
------------------------
1 10
2 10
3 10
4 10
The only solution I see is something like this:
SET id1 = (Select List_ID From Table_A Where ID = _ID_Param1);
SET id2 = (Select List_ID From Table_A Where ID = _ID_Param2);
SET id3 = (Select List_ID From Table_A Where ID = _ID_Param3);
SET id4 = (Select List_ID From Table_A Where ID = _ID_Param4);
#Then I have to check if all ids are same
IF id1 = id2 = id3 = id4 THEN I found the same List_ID
And sometime _ID_Param is set as NULL, so I might have only 1 or 2 or all 4
Sorry If I'm not explaining this very well... but I don't know how to tell this situation, and my knowledges are limited, I need some help
UPDATE
this is close to what I need:
SELECT
IF( (
MIN( List_ID ) = MAX( List_ID )
AND COUNT( * ) = (Select Count(*) From (SELECT _ID_Param1 AS val
UNION ALL
SELECT _ID_Param2
UNION ALL
SELECT _ID_Param3
UNION ALL
SELECT _ID_Param4) Temp Where Temp.val is not null
) ) , List_ID, NULL
) AS LID
FROM table_a
WHERE ID IN ( _ID_Param1, _ID_Param2, _ID_Param3, _ID_Param4 )
The Only Wrong thing is that it will return the List_ID even if a parameter not exists in table and there is a duplicate of other parameters And it count it twice. How to exclude such case, to check if all params exists
You can do this in one query:
select #all_same := (min(List_ID) = max(List_ID) and count(*) = 4)
from table_a
where id in (_ID_Param1, _ID_Param2, _ID_Param3, _ID_Param4);
This assumes that each id is unique in table_a. That is how your queries are set up.
You can use the COUNT aggregate function to find tuples
Select List_ID, COUNT(*) as tuple
FROM Table_A
WHERE ID = _ID_Param_1 OR ID = _ID_Param_2 ... etc
GROUP BY List_ID
HAVING COUNT(*) = 4;
This will return List_IDs that appears 4 times for a given set of parameters.

Avoid Subquery returned more than 1 value error in a table valued function

Is there a way to rewrite this query without getting error?: Subquery returned more than 1 value.
This is query is used in a LEFT JOIN in a table-valued function. Per requirement, I need to by default pull two scenario IDs (if parameter value is NULL or empty)
DECLARE #pScenarioName AS VARCHAR(30)
select
externalID,
PropertyAssetId,
LeaseID,
BeginDate
from ae11.dbo.ivw_Leases
WHERE PropertyAssetID IN
(select ID from AE11.dbo.PropertyAssets where scenarioID IN
(CASE WHEN isnull(#pScenarioName, '') = ''
THEN (select top 2 ID from rvw_Scenarios where Name like '[0-9][0-9][0-9][0-9]%'
AND LEN(Name) = 8
order by Name desc)
ELSE
(select ID from aex.dbo.rvw_Scenarios
where [Name] IN (#pScenarioName))
END)
)
I haven't tested this, but I use a similar approach when dealing with parameters. Of course, this won't necessarily work if the order of the ID is crucial in your second subquery.
SELECT ExternalID
,PropertyAssetId
,LeaseID
,BeginDate
FROM ae11.dbo.ivw_Leases
WHERE PropertyAssetID IN
(SELECT ID
FROM AE11.dbo.PropertyAssets
WHERE scenarioID IN
(SELECT TOP 2 ID
FROM rvw_Scenarios
WHERE (#ISNULL(#pScenarioName,'') = ''
AND Name LIKE '[0-9][0-9][0-9][0-9]%'
AND LEN(Name) = 8)
ORDER BY Name DESC
UNION ALL
SELECT ID FROM aex.dbo.rvw_Scenarios
WHERE (#pScenarioName IS NOT NULL)
AND [Name] IN (#pScenarioName)))

How to select the first row from a query into a procedure-variable in MySQL

DECLARE topScorer INT default 0;
SELECT id INTO topScorer FROM game_player
WHERE game_player.score = (SELECT max(score) FROM game_player)
A bad example but one that could easily result from naive coding... it doesn't work in my testing if multiple rows are returned, how can I get the first returned row into the variable?
Do you need just the one score?
SELECT id
INTO topScorer
FROM game_player
WHERE game_player.score = ( SELECT max(score) as maxScore
FROM game_player
) LIMIT 1
Update:
Sir Rufo was right, the code above has now been corrected.
Apply limit in sub query to get only 1 value from sub query
SELECT id
INTO topScorer
FROM game_player
WHERE game_player.score = ( SELECT max(score)
FROM game_player LIMIT 1 );
Or to get multiple value from sub query used below one:
SELECT id
INTO topScorer
FROM game_player
WHERE game_player.score in ( SELECT max(score)
FROM game_player );
Use LIMIT x to ensure you are receiving only x rows from your query.
In this case you only want to get 1 row:
SELECT id
INTO topScorer
FROM game_player
WHERE game_player.score = ( SELECT max(score)
FROM game_player )
LIMIT 1
SQL Fiddle DEMO
As a working alternative you can also use this
SELECT id
INTO topScorer
FROM game_player
ORDER BY score DESC
LIMIT 1
SQL Fiddle DEMO
1) declare variable in SP:
declare #CourseID int
set #CourseID = 0
2) We need two query first for assign ID to variable and inner query for select only Top 1 record form table. In where clause of first query we compare ID with result of inner query:
SELECT #CourseID = ID FROM Course ID = ( Select Top 1 ID from Course )
3) Now check Variable value:
if(#CourseID > 0 )
Begin
//This mean ID of first row is assigned to CourseID
End
Else
Begin
//Can't found any record.
End

return null row in mysql if record not found for the given id

Hi am using the below mysql query
SELECT *
FROM particulars pp
WHERE (pp.SnoFK IN (108,999999)
AND pp.curMonth = STR_TO_DATE('01/02/2012', '%d/%m/%Y'))
In my table i have record for only 108, so it returns only one row for 108.
Is there any other option in mysql that can i return two rows which i dont have the id in the table like
1.108 | *
2.999999 | null values
I have no better idea:
http://sqlfiddle.com/#!2/82cc5/2
SELECT
ids.id,
particulars.*
FROM ( SELECT 108 AS id
UNION SELECT 1122 AS id
UNION SELECT 999999 AS id
) AS ids -- create a "table" with the required numbers
LEFT JOIN particulars ON particulars.SnoFK = ids.id