I want to check the value of subquery how to check it? - mysql

This is my update query in mysql
UPDATE userentries
SET User_Rank =
(
SELECT
urc.rankofentr as User_Rank
from UserRankingsys as urc
WHERE urc.entryid = userentries.id
LIMIT 1
)
where userentries.id = 15
I want to check the value which is returned by that section
(
SELECT
urc.rankofentr as User_Rank
from UserRankingsys as urc
WHERE urc.entryid = userentries.id
LIMIT 1
)
how i can do this if this sub query returns greater than 20 than i want to set the value 102

You can use CASE
SELECT case urc.rankofentr when > 20 then 20
else urc.rankofentr
end as User_Rank
from UserRankingsys as urc
WHERE urc.entryid = userentries.id
LIMIT 1
for you sql
UPDATE userentries
SET User_Rank =
(
SELECT case urc.rankofentr when > 20 then 20
else urc.rankofentr
end as User_Rank
from UserRankingsys as urc
WHERE urc.entryid = userentries.id
LIMIT 1
)
where userentries.id = 15;

Related

Getting the latest row from status

Supposed I have a table with the following rows
test_no
test_count_no
test_status
test_run_no
1
0
STOP
1
2
66
FINISH
1
3
67
FINISH
1
4
0
STOP
2
5
0
STOP
2
I need to get the row which has a test_status of FINISH and has the latest test_no,
for example test_run_no=1 since there is two test_status of FINISH, I only need to get the latest test_no which is 3
In case of test_run_no=2 since there is no test_status=FINISH i only need to return the first row.
The result will be:
test_no
test_count_no
test_status
test_run_no
3
67
FINISH
1
4
0
STOP
2
My current query is
SELECT MAX(test_table.test_run_no)
FROM test_table
WHERE test_table.run_status = 'FINISH'
GROUP BY test_table.test_no
Any idea on what query should I add in order to achieve the result ?
A correlated subquery fits this:
select t.*
from test_table t
where t.test_no = (select t2.test_no
from test_table t2
where t2.test_run_no = t.test_run_no
order by (test_status = 'FINISH') desc,
(case when test_status = 'FINISH' then test_no end) desc,
test_no asc
);
The order by has three keys for your conditions:
Put 'FINISH' first.
For 'FINISH' get the largest test_no.
For others, get the smallest.
This logic can also be expressed using row_number():
select t.*
from (select t.*,
row_number() over (partition by test_run_no
order by (test_status = 'FINISH') desc,
(case when test_status = 'FINISH' then test_no end) desc,
test_no asc
) as seqnum
from test_table t
) t
where seqnum = 1;
What about the following statement:
select * from test t where t.test_status = 'FINISH'
and t.test_no = (select max(t1.test_no) from test t1
where t1.test_run_no = t.test_run_no)
union
select * from test t where t.test_status = 'STOP'
and not exists (select 1 from test t2 where t2.test_run_no = t.test_run_no
and t2.test_status = 'FINISH')
and t.test_no = (select min(test_no) from test t1
where t1.test_run_no = t.test_run_no);
It consists of two SELECT parts. In the first part, for the tests that have a FINISH status, the one with the highest test number is selected.
The second part selects the smallest test number for the tests for which there is no run with the status FINISH.
Is this what you are looking for
SELECT MAX(test_table.test_run_no) FROM test_table
where test_table.run_status = 'FINISH' and test_run_no=1
GROUP BY test_table.test_no
IF ##ROWCOUNT = 0
BEGIN
select MAX(test_table.test_run_no) from test_table GROUP BY test_table.test_no
END
try it :
select * from Table_3 where Table_3.test_count_no = (
select max(test_count_no) from Table_3
) and Table_3.test_status = 'FINISH' and Table_3.test_run_no = 1
union all
select x.test_no, x.test_count_no, x.test_status, x.test_run_no from
(select ROW_NUMBER() over (order by Table_3.test_run_no) as rownum, Table_3.* from Table_3 where Table_3.test_run_no = 2) as x where x.rownum = 1
Use order by desc and use limit 1 to get the exact row.
Like this
SELECT *
FROM test_table
WHERE test_table.test_status = 'FINISH'
ORDER BY test_no desc
LIMIT 1
if you need all the rows
SELECT *
FROM test_table
WHERE test_table.test_status = 'FINISH'
ORDER BY test_no desc
if you need test_no only
SELECT test_table.test_no
FROM test_table
WHERE test_table.test_status = 'FINISH'
ORDER BY test_no desc
LIMIT 1

MYSQL CASE/IF over a value selected with SELECT

I'd like to run a CASE statement or IF on the COUNT_A returned by the select query below and set the value of a variable A_VAL.
SELECT A_DATE, COUNT(A_INS_NAM) AS COUNT_A
FROM TABLE1
WHERE A_INS_NAM IN
(
SELECT A_INS_NAM FROM IWD
WHERE I_ID IN
(
SELECT IM_ID FROM TIM WHERE IM_ID = (
SELECT T_ID FROM TWS WHERE TN = 'abced')
)
) AND A_DATE BETWEEN '2014-01-01' AND '2015-05-01' AND A_INS_NAM NOT LIKE '%pk%'
I'd like the output to have 2 columns namely A_DATE, A_VAL. The value of A_VAL gets set based on
If COUNT_A = 10, then A_VAL = 1
If COUNT_A = 20, then A_VAL = 2
If COUNT_A between 30 & 50, then A_VAL = 3
If COUNT_A > 50, then A_VAL = 5
Could I get someone's help please?
You should use joins instead of sub queries for better performance something like below, also you are using an aggregate function without providing grouping criteria so it will result as a single row for this i have added GROUP BY t.A_DATE in below query
SELECT
t.A_DATE,
CASE
WHEN COUNT(t.A_INS_NAM) = 10 THEN 1
WHEN COUNT(t.A_INS_NAM) = 20 THEN 2
WHEN COUNT(t.A_INS_NAM) BETWEEN 30 AND 50 THEN 3
WHEN COUNT(t.A_INS_NAM) > 50 THEN 5
ELSE 'some value'
END AS A_VAL
FROM
TABLE1 t
JOIN IWD t1 ON(t.A_INS_NAM = t1.A_INS_NAM)
JOIN TIM t2 ON(t1.IWD = t2.IM_ID)
JOIN TWS t3 ON(t2.IM_ID = t3.T_ID )
WHERE t3.TN = 'abced'
AND t.A_DATE BETWEEN '2014-01-01' AND '2015-05-01'
AND t.A_INS_NAM NOT LIKE '%pk%'
GROUP BY t.A_DATE

SQL debug - where condition ignored

Can somebody explain me this. My SQL:
SELECT
`offers`.`id`,
`offers`.`max_available`,
(SELECT COUNT( coupons.id ) FROM coupons WHERE coupons.status = 'Y' AND coupons.offer_id = offers.id) AS coupons_sold
FROM
`offers`
WHERE
`offers`.`status` IN ('P', 'S') AND
`offers`.`published_at` < 1341612000 AND
`offers`.`end_at` >1341567914 AND
`coupons_sold` < `offers`.`max_available`
ORDER BY `offers`.`created_at` DESC
LIMIT 4 OFFSET 0
This will return me these 4 rows:
id max_available coupons_sold
195 19 20
194 9999 0
193 9999 0
159 9999 93
How is possible that row with ID 195 is included, if I have this condition in where coupons_sold < offers.max_available? I am clueless!
This query would produce an error, as you can't use in WHERE clause, an alias from the SELECT list. Unless table offers has a coupons_sold column, too!
Try this query, instead:
SELECT id, max_available, coupons_sold
FROM
( SELECT
`offers`.`id`,
`offers`.`max_available`,
( SELECT COUNT( coupons.id )
FROM coupons
WHERE coupons.status = 'Y'
AND coupons.offer_id = offers.id
) AS coupons_sold
offers.created_at
FROM
`offers`
WHERE
`offers`.`status` IN ('P', 'S') AND
`offers`.`published_at` < 1341612000 AND
`offers`.`end_at` >1341567914
) AS tmp
WHERE coupons_sold < max_available
ORDER BY created_at DESC
LIMIT 4 OFFSET 0 ;

how to put if else condition in LIMIT mysql

here is my query
$where_or = "
AND
content_status = 'Active'
AND
content_post_status = 'published'
AND
deleted = 0
AND
content_type = '$type'
ORDER BY content_created_at DESC ";
$select = "content_id, content_title, content_short_description, content_url, content_created_at";
$query = $this->db->query("
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 54
$where_or
LIMIT 3
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 55
$where_or
LIMIT 2
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 56
$where_or
LIMIT 2
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 57
$where_or
LIMIT 1
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 58
$where_or
LIMIT 1
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 60
$where_or
LIMIT 1
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 61
$where_or
LIMIT 1
)
UNION ALL
(
SELECT $select
FROM tbl_content
WHERE
content_category_id = 118
$where_or
if content_type = 'article'
begin
LIMIT 10
end
)
");
You can see in last UNION i want to put limit on if condition ..but its giving me error...how can i do that...please help..
You cannot do that with MySQL, at least not the way you are trying to. If you think about it
this:
if content_type = 'article'
begin
LIMIT 10
end
doesn't make sense. LIMIT is applied to a set not a single record. If what you wrote were valid SQL and your data contained both content that was true for content_type = 'article' and some that was false. What would you expect to happen?
I'd suggest you to rank records and then add WHERE condtition to filter records by content_category_id and its rank. In this case you will avoid using multiple SELECT...FROM queries.

save result of UNION query in local variable in MYSQL stored procedure

In my MySQL Stoared Procedure I want to store result of bellow query into local variables.
MySQL SP
BEGIN
Declare temp_ID bigint;
Declare temp_teamName Text;
(select ID,team1 from tbl_tournament_matches where leveID = 1 and tournamentID = 91 and matchType = 'L')
UNION
(select ID,team1 from tbl_tournament_matches where leveID = 2 and tournamentID = 91 and looserTeam is not null)
ORDER BY RAND() LIMIT 0,1;
select temp_ID, temp_teamName;
END;
How can I pass result of query into local variable?
note : above SP will return only 1 row.
You can achieve this without having to store the value into a variable.
SELECT * FROM(
select ID,team1 from tbl_tournament_matches where leveID = 1 and tournamentID = 91 and matchType = 'L'
UNION
select ID,team1 from tbl_tournament_matches where leveID = 2 and tournamentID = 91 and looserTeam is not null
) ORDER BY RAND() LIMIT 0,1
But, if you want to store a value for later use, you can use the INTO keyword:
SELECT id, data INTO #x, #y FROM test.t1 LIMIT 1;
http://dev.mysql.com/doc/refman/5.0/en/select-into.html