Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamical SQL-Query over different tables 1

Status
Not open for further replies.

bateman23

Programmer
Mar 2, 2001
145
DE
Hello i've got a big, big PHP, SQL-search statement problem. I've got a lot of tables looking like that:

Code:
[b]projekte:[/b]
nr: INT
name: INT
foerder_de: TEXT
foerder_en: TEXT
... and more...

[b]acronym:[/b]
id_acronym: INT
name: VARCHAR

[b]link_acronym[/b]
id_link_acronym: INT
id_acronym: INT
projekte_nr: INT

other tables, the same style, like
keyword, linkkeyword, orga, link_orga, and so on. The link-Table - you may guess - link the data-set with a project. (So, one project may have lot of acronyms or keywords)

Now i need to create a search-mask. The user should e.g. be able to search for all projekts which got a specific value in the text-field "foerder_de" OR "foerder_en") AND have the acronym "xyz" AND the keyword "test" or "test2". They also should be able change the AND with a OR-conjunction, add other conjunctions, and so on...

First problem: I need to create the sql-query absolutly dynamical. - I think i could get that...
the bigger problem is. I don't know how my sql-statement should look like. My first idea was just this:

Code:
SELECT * from projekte, acronym WHERE (acronym.name = "test" || acronym.name = "testing") && (projekte.foerder_de = "rahmen" || projekte.foerder_en = "rahmen" || projekte.foerder_dritte = "rahmen")

..and then add other Where clauses dynamically.
But, that isn't working. so i tried the following, which isn't working at all.
I get too many results and other results - i need to get - dont show up...

Code:
SELECT * 
FROM projekte 
LEFT OUTER JOIN link_acronym, acronym ON
acronym.id_acronym = link_acronym.id_acronym &&
acronym.name = "test" || acronym.name = "testing"
WHERE (projekte.foerder_de = "rahmen" || projekte.foerder_en = "rahmen" || projekte.foerder_dritte = "rahmen")

Any ideas about my sql-querys are welcome. I have no idea how to go on.
Thanks a lot in advance,
Daniel Purucker
 
Code:
select projekte.* 
  from projekte 
inner
  join link_acronym
    on projekte.nr
     = link_acronym.projekte_nr
inner
  join acronym 
    on link_acronym.id_acronym
     = acronym.id_acronym 
   and acronym.name in ( 'test' , 'testing' )
 where (
       projekte.foerder_de = 'rahmen' 
    or projekte.foerder_en = 'rahmen' 
    or projekte.foerder_dritte = 'rahmen'
       )
if your foerder_xx columns are TEXT, it is unlikely that they will be equal to a single word, so you might want to try
Code:
 where (
       projekte.foerder_de like '%rahmen%' 
    or projekte.foerder_en like '%rahmen%' 
    or projekte.foerder_dritte like '%rahmen%'
       )

tip: don't use || and &&, use the standard sql keywords AND and OR

tip: use singlequote to delimit strings, not doublequote



rudy
SQL Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top