SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Bangalore, India
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL conditions for the select Query

    Hi,

    I am new to MySQl and not sure if this is possible or not. Let me put it across for some help. I would like to have a query which
    can check a condition and decide on one of the two 'select' queries to be executed. Some thing like this,

    Code:
    if(select count(*) as test_count from test_table > '0' , select query on True , select query on false);
    1) Can something like this be done in a single query?
    2) If yes, how can I do it exactly?

    Thanks in Advance
    Thank You
    Known is a Drop, Unknown is an Ocean

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    without knowing what your two queries look like, i will guess that it would probably be better to do the logic with your application language (e.g. php)

    p.s. did you know we have a special forum just for mysql?

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Bangalore, India
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    without knowing what your two queries look like, i will guess that it would probably be better to do the logic with your application language (e.g. php)
    Yes, that is definitely one option. But I thought if all that could happen in a single query then it would be better.

    Sorry if it was not clear. Let me explain ,

    I have two tables,

    Table 1: with fields is_valued and name

    The table has this data :

    1 - name1
    1 - name2
    0 - name3
    1 - name4
    0 - name5


    Table 2: with fields is_valued and name

    The table has this data :

    1 - name1
    1 - name2
    1 - name3
    1 - name4
    1 - name5

    So, i want a single select query that can give me all data of table1 or of table2 based on whether 'count(*) from table1 where is_valued > 0'
    is true or false respectively.
    Thank You
    Known is a Drop, Unknown is an Ocean

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by vigneshr35 View Post
    So, i want a single select query that can give me all data of table1 or of table2 based on whether 'count(*) from table1 where is_valued > 0'
    is true or false respectively.
    use two queries
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Bangalore, India
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    use two queries
    Can you please explain a bit clearly?
    Thank You
    Known is a Drop, Unknown is an Ocean

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    first query:
    Code:
    SELECT COUNT(*) FROM table1 WHERE is_valued > 0
    then, depending on the returned count, you would run either the query for table1 or the query for table2
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Location
    Bangalore, India
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    first query:
    Code:
    SELECT COUNT(*) FROM table1 WHERE is_valued > 0
    then, depending on the returned count, you would run either the query for table1 or the query for table2
    I had implemented the same before I wanted to know if we can do something like that in a single query. I know believe that is the only way we can do what i want. Thanks again
    Thank You
    Known is a Drop, Unknown is an Ocean


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •