Results 1 to 5 of 5

Thread: How can I convert 2 queries into single query

  1. #1
    Join Date
    Feb 2008
    Posts
    1,260

    How can I convert 2 queries into single query

    I have the following table and I try to extract all lines with the B value for the fields RefD and all lines whose RefA fields to the value B and the value of their RefD fields is among those with value B for the fields RefD. If my table contains:
    RefA ..... RefD
    ..K ........W
    ..Z ....... E
    ..A ........ B
    ..C ........ B
    D ........ B
    B ........ C
    B ........ H

    the result is that waiting

    RefA RefD ....
    A...... B
    C ..... B
    D ..... B
    B ..... C


    At the moment I made the two requests
    the first on my table
    Code:
    SELECT * FROM TABLE WHERE RefD=B
    And the second on the outcome of the requests
    Code:
    SELECT * FROM TABLE WHERE RefD=A OR RefD=C OR RefD=D
    I want to know if it is possible to do this in one query.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How can I convert 2 queries into single query

    When you write
    extract all lines with the B value for the fields RefD and all lines whose RefA fields to the value B and the value of their RefD fields is among those with the value B for the fields RefD.
    I think from reading your example and your code that you wanted to write:

    extract all lines with the B value for the fields RefD and all lines whose RefA fields to the value B and the value of their RefD fields is among those not having value B for the fields RefD.
    If so:

    Code:
      SELECT *  
      FROM TABLE  
      WHERE RefD = 'B' 
      OR (RefA = 'B' AND RefD <> 'B')

  3. #3
    Join Date
    May 2008
    Posts
    271

    Re: How can I convert 2 queries into single query

    A union is sufficient

    Code:
    SELECT * FROM TABLE WHERE RefD=B
    UNION 
    SELECT * FROM TABLE WHERE RefD IN (A, C, D)

  4. #4
    Join Date
    Feb 2008
    Posts
    1,260

    Re: How can I convert 2 queries into single query

    @ Modifier

    No it does not work because if you apply this solution to my example the line
    "B ........ H" will appear which is not needed.

    @ XSI

    Except that I do not know the values A, C, D in advance, that is why I make 2 requests at this time.

  5. #5
    Join Date
    May 2008
    Posts
    271

    Re: How can I convert 2 queries into single query

    Use a subquery:

    Code:
    SELECT * FROM TABLE  
      WHERE RefD = B OR (RefA=B AND RefD IN ( SELECT RefD FROM TABLE WHERE RefD=B))

Similar Threads

  1. Want to make query dependent on another query.
    By MACE in forum Software Development
    Replies: 4
    Last Post: 01-02-2010, 05:22 PM
  2. MySQL query in a query
    By Rail racer in forum Software Development
    Replies: 3
    Last Post: 21-07-2009, 07:06 PM
  3. Turn on MySQL query cache to speed up query performance
    By DMA2Superman in forum Software Development
    Replies: 3
    Last Post: 07-07-2009, 10:26 AM
  4. Need to have multiple where queries in MySQL query
    By Anti_Trend in forum Software Development
    Replies: 1
    Last Post: 03-07-2009, 10:17 PM
  5. Convert Mulitple .avi into Single DVD
    By ARTHUR18 in forum Windows Software
    Replies: 3
    Last Post: 16-06-2009, 11:15 PM

Tags for this Thread

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
  •  
Page generated in 1,714,158,611.35884 seconds with 16 queries