how to put if else condition in LIMIT mysql - 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.

Related

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

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;

1242 Subquery returns more than 1 row

I got this error, I hope you may help me. I want to show a certain item in a search.
SELECT p.id, p.property_rank, p.pic_numb, p.att_numb, p.confirm, p.finalized ,p.deleted, p.user_id, p.add_date, p.visit_time,p.visit_date,p.sent_numb, p.contact_numb, zip_name, zip_id, p.street, p.sp_featured, p.property_title, p.b_price_unit, p.b_price_si, p.b_price, p.b_price, p.street_no, p.field_54,
p.field_409,
( SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` = (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`)
) AS `listing_type_id`,
p.listing, p.googlemap_ln, p.googlemap_lt, p.category, p.b_bedrooms, p.b_bathrooms, p.sp_openhouse, p.b_price_period, p.b_lot_area_unit, p.b_lot_area_si, p.b_lot_area, p.b_lot_area, p.b_living_area_unit, p.b_living_area_si, p.b_living_area, p.b_living_area, p.description, p.sp_hot, p.sp_forclosure
FROM res_rpl_properties AS p
WHERE 1 AND p.`type` = '0' AND p.`confirm` = '1' AND p.`finalized` = '1' AND p.`deleted` = '0' AND p.`category` IN(9,8,10)
ORDER BY p.add_date DESC
LIMIT 0 , 12
The error is telling you that your subquery (selected as listing_type_id) returns more than one row. To rephrase - it's returning more than one value for listing_type_id. You should limit the results from the subquery to just one.
You have two options:
OR select just the first row of subquery
(SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` = (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`
LIMIT 1
)
LIMIT 1
) AS `listing_type_id`
OR use IN to allow multiple comparation
(SELECT `listing_type`.`id`
FROM `res_rpl_listing_types` AS `listing_type`
WHERE `listing_type`.`id` IN (
SELECT `listing`.`type`
FROM `res_rpl_listings` AS `listing`
WHERE `listing`.`id` = p.`listing`)
LIMIT 1
) AS `listing_type_id`
The problem is there is 2 subqueries and you need to treat both. Both of them need to be limited to 1 row only.

How i can realize the next mysql query on zend?

SELECT * FROM (
SELECT `* FROM `messages`
ORDER BY `message_id` DESC
LIMIT 15
) AS temp
ORDER BY message_id ASC
$sub_select = $db->select();
$sub_select->from('messages');
$sub_select->order('message_id desc');
$sub_select->limit(15);
$select = $db->select();
$select->from(array('temp' => new Zend_Db_Expr('(' . $sub_select . ')')));
$select->order('message_id asc');
Doc : http://framework.zend.com/manual/1.12/en/zend.db.select.html

Get last X rows using LIMIT and ORDER BY col ASC

This query is to retrieve some messages, it retuns all of them:
$q = "
SELECT *
FROM pms
WHERE
(
(id_to = $id and id_from = ".sesion().")
OR
(id_from = $id and id_to = ".sesion().")
)
AND (id > $from)
ORDER by fecha ASC";
The thing is i would like to get the last 50 elements, but I think its ony posible using DESC ordering..
how can i do it?
Do i need to count first how many rows so then can I use LIMIT $many-$ipp,$many ? or is there a way to invert the result order?
Just make your query a subquery:
SELECT * FROM
(
SELECT *
FROM pms
WHERE
(
(id_to = $id AND id_from = ".sesion().")
OR (id_from = $id and id_to = ".sesion().")
)
AND id > $from
ORDER BY fecha DESC
LIMIT 50
) q1
ORDER BY fecha ASC

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 ;