1

I have 2 select statements.

1- select sm.id, IFNULL(sm.unit_price,0) as unit_price       
   from crm_stock_manager sm
   left join crm_purchase_order_items poi on poi.id = sm.purchase_order_item_id and  poi.is_del = 0
   left join crm_products p on poi.product_id = p.id and p.is_del = 0
   where sm.is_del = 0 and IFNULL(sm.unit_price, '') != '' and p.id = 253 order by sm.created_on;

2- select 0 as id ,price as unit_price from crm_products where id = 253  and is_del=0;

If 1st select statement return an empty result, then i want to run 2nd select statement.

Please help me.

6
  • 1
    Just perform 2 separated queries, one after another Commented Feb 27, 2013 at 4:55
  • Is there any possible ways to do this on mysql ? Commented Feb 27, 2013 at 5:01
  • 2
    is there any reason to do that in mysql? Commented Feb 27, 2013 at 5:05
  • Does this mean that you need to select crm_products.price if srm_stock_manager.unit_price is NULL? Commented Feb 27, 2013 at 5:44
  • Noop. if crm_stock_manager table contains any data i need to take the records from that table. other wise it will take from crm_products table. Commented Feb 27, 2013 at 6:35

3 Answers 3

5

This appears to work from a quick test I just did and avoids the need to check for the existence of x=1 twice.

SELECT SQL_CALC_FOUND_ROWS *
FROM mytable
WHERE x = 1

UNION ALL

SELECT *
FROM mytable
WHERE 
FOUND_ROWS() = 0 AND x = 2;

Another way is Subqueries with EXISTS or NOT EXISTS

SELECT column1 FROM t1 WHERE NOT EXISTS (SELECT * FROM t2);
Sign up to request clarification or add additional context in comments.

3 Comments

Has the behavior of FOUND_ROWS() across SELECTs in the same UNION query changed? I'm seeing this method working for MySQL 5.1.61 but not for 5.6.15. In the latter case, FOUND_ROWS() in the second SELECT seems to return for the previous query instead of for the previous SELECT. Am I missing something?
Their is great difficulty between union and found rows but it depends on need and table schema
the not exists is a nice solution
1

You can do it like this

$query1 = mysql_query("YOUR QUERY");
$row_count = mysql_num_rows($query1);
if($row_count==0) {
 $query1 = mysql_query("Your Second Query");
}

Remember to not use mysql_* functions as they are depreciated.

Hope This Helps

Comments

1

If you need to select crm_products.price if srm_stock_manager.unit_price is NULL, you can use COALESCE and you need not 2 queries. You can change your first part of SELECT statement as follows and other part as necessary to get the result required.

SELECT sm.id, COALESCE(sm.unit_price, p.price) unit_price FROM...

This returns p.price value if sm.unit_price is NULL.

Further, refer mysql doc.

1 Comment

IFNULL or COALESCE. Refer this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.