• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Executing SQL query based on certain condition

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to get data from a table based on certain condition. For eg, if table1 is not empty then get complete data from table2 else get the data from table2 for which created_dt (let's say it's one of the columns in table2) is today's date. It's like following :

if(condition)
then execute query1
else
execute query2

Is there any way to achieve this in SQL?
 
Whizlabs Java Support
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harish V Gupta,

You can use count(*) method to know the number of rows depending on that result you can write the query.

Regardss,
James
 
Harish V Gupta
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply James. You are right that count(*) will give me total number of records. But I am not able to figure out how to use that value in a condition for choosing between queries.
 
Ranch Hand
Posts: 108
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand correctly, I think you are trying to evaluate condition at the database layer. Isn't the condition a part of business layer ? The job of the lowest layer of ORM must be to execute a query. Which query has to be executed must be decided at the business layer. In short, you can use count(*) to evaluate the ccondition and use the ORM to execute the query.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic