Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. multiple combination searching query

multiple combination searching query

Scheduled Pinned Locked Moved Database
databasealgorithmshelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dream_liner_7e7
    wrote on last edited by
    #1

    i have a table of a library which contain several fields like author, publisher, isbn number etc etc. I want to write a search query in different combination if user select only author then its return result according to author, if user select multiple fields in any combination then the query must return result according to that. Can any1 help me? Thanks in advance.

    C 1 Reply Last reply
    0
    • D dream_liner_7e7

      i have a table of a library which contain several fields like author, publisher, isbn number etc etc. I want to write a search query in different combination if user select only author then its return result according to author, if user select multiple fields in any combination then the query must return result according to that. Can any1 help me? Thanks in advance.

      C Offline
      C Offline
      ChandraRam
      wrote on last edited by
      #2

      dream_liner_7e7 wrote:

      i have a table of a library which contain several fields like author, publisher, isbn number etc etc. I want to write a search query in different combination if user select only author then its return result according to author, if user select multiple fields in any combination then the query must return result according to that. Can any1 help me?

      What have you tried so far? If you can post the query that you now have, and also tell us where you are stuck, people here will be more than willing to try and help you.

      D 1 Reply Last reply
      0
      • C ChandraRam

        dream_liner_7e7 wrote:

        i have a table of a library which contain several fields like author, publisher, isbn number etc etc. I want to write a search query in different combination if user select only author then its return result according to author, if user select multiple fields in any combination then the query must return result according to that. Can any1 help me?

        What have you tried so far? If you can post the query that you now have, and also tell us where you are stuck, people here will be more than willing to try and help you.

        D Offline
        D Offline
        dream_liner_7e7
        wrote on last edited by
        #3

        SELECT [AIMS_Library.Reck].Reck_Code + ' ' + [AIMS_Library.Reck].Shelf_Code AS [Reck & Shelf], [AIMS_Library.Category].Category_Name, [AIMS_Library.BookDetails].Qty, [AIMS_Library.BookDetails].ISBN, [AIMS_Library.BookDetails].BookTitle, [AIMS_Library.BookDetails].Author, [AIMS_Library.BookDetails].Publisher, [AIMS_Library.BookDetails].Edition, [AIMS_Library.BookDetails].YearPublished FROM [AIMS_Library.BookDetails], [AIMS_Library.Category], [AIMS_Library.Reck] where [AIMS_Library.Reck].reck_id = [AIMS_Library.BookDetails].reck_id and [AIMS_Library.Category].category_id = [AIMS_Library.BookDetails].category_id and [AIMS_Library.BookDetails].author = 'abc' or [AIMS_Library.BookDetails].publisher = 'xyz' the last line thats where i stuck, when from my c# form users will try to search books. Is there any 1 query solution or i have to write multiple queries? Thanks in advance.

        C W 2 Replies Last reply
        0
        • D dream_liner_7e7

          SELECT [AIMS_Library.Reck].Reck_Code + ' ' + [AIMS_Library.Reck].Shelf_Code AS [Reck & Shelf], [AIMS_Library.Category].Category_Name, [AIMS_Library.BookDetails].Qty, [AIMS_Library.BookDetails].ISBN, [AIMS_Library.BookDetails].BookTitle, [AIMS_Library.BookDetails].Author, [AIMS_Library.BookDetails].Publisher, [AIMS_Library.BookDetails].Edition, [AIMS_Library.BookDetails].YearPublished FROM [AIMS_Library.BookDetails], [AIMS_Library.Category], [AIMS_Library.Reck] where [AIMS_Library.Reck].reck_id = [AIMS_Library.BookDetails].reck_id and [AIMS_Library.Category].category_id = [AIMS_Library.BookDetails].category_id and [AIMS_Library.BookDetails].author = 'abc' or [AIMS_Library.BookDetails].publisher = 'xyz' the last line thats where i stuck, when from my c# form users will try to search books. Is there any 1 query solution or i have to write multiple queries? Thanks in advance.

          C Offline
          C Offline
          ChandraRam
          wrote on last edited by
          #4

          One way to solve this would be to dynamicallyl create the query based on user selection... keep the basic query (including fields needed and joins) a constant, and add to the query string for each selection made. So, if user has selected author 'xyz' and publisher 'abc' qry = [basic query here, including the joins] if [author selected] then qry = constant + " and author=" + selected author if [publisher selected] then qry = constant + " and publisher=" + selected publisher ... and so on You might want to look into creating parameters for your SQL query, to prevent injection attacks. HTH

          D 1 Reply Last reply
          0
          • D dream_liner_7e7

            SELECT [AIMS_Library.Reck].Reck_Code + ' ' + [AIMS_Library.Reck].Shelf_Code AS [Reck & Shelf], [AIMS_Library.Category].Category_Name, [AIMS_Library.BookDetails].Qty, [AIMS_Library.BookDetails].ISBN, [AIMS_Library.BookDetails].BookTitle, [AIMS_Library.BookDetails].Author, [AIMS_Library.BookDetails].Publisher, [AIMS_Library.BookDetails].Edition, [AIMS_Library.BookDetails].YearPublished FROM [AIMS_Library.BookDetails], [AIMS_Library.Category], [AIMS_Library.Reck] where [AIMS_Library.Reck].reck_id = [AIMS_Library.BookDetails].reck_id and [AIMS_Library.Category].category_id = [AIMS_Library.BookDetails].category_id and [AIMS_Library.BookDetails].author = 'abc' or [AIMS_Library.BookDetails].publisher = 'xyz' the last line thats where i stuck, when from my c# form users will try to search books. Is there any 1 query solution or i have to write multiple queries? Thanks in advance.

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            What ChandraRam suggested is a good way to go. However, also notice that you may have a potential problem in your query. Since you have an OR in the middle of the conditions, but no parenthesis used the result may be unexpected and in your example lead to cartesian product.

            The need to optimize rises from a bad design

            1 Reply Last reply
            0
            • C ChandraRam

              One way to solve this would be to dynamicallyl create the query based on user selection... keep the basic query (including fields needed and joins) a constant, and add to the query string for each selection made. So, if user has selected author 'xyz' and publisher 'abc' qry = [basic query here, including the joins] if [author selected] then qry = constant + " and author=" + selected author if [publisher selected] then qry = constant + " and publisher=" + selected publisher ... and so on You might want to look into creating parameters for your SQL query, to prevent injection attacks. HTH

              D Offline
              D Offline
              dream_liner_7e7
              wrote on last edited by
              #6

              thank you every1 thats a valuable solution

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups