[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Den tomme mængde i SQL.



I sslug.programmering, skrev Michael Rasmussen:
>  On Wed, 04 Jan 2006 19:58:42 +0000, Jesper Krogh wrote:
> > Jeg ville gerne have det til at virke i et "helt generelt" tilfælde.. 
> > 
>  Jeg antager, at id er tabellens primary key, hvorfor følgende altid vil
>  returnere den tomme mængde:
> 
>  select null from table where id is null
>  eller
>  select null from table where not id

Det er et tricky spørgsmål, men jeg har ingen problemer med at lave en
query der giver et tomt (where id != id vil altid gøre det).

Problemet er at generere SQL automatisk når et af felterne skal være i
en liste der potentielt er tom.

select id from table where id in (1,2,3); -> Virker og kan fint give et
tomt resultat. mit array indeholder (1,2,3) og SQL laves med:

select * from table where id in (join(",",@arr));

.. altså.. omskriv ovenstående SQL/perl-kode så det også giver et tomt
resultatsæt når @arr = ().. 

$ perl -e'print "select id from table where id in (" . join(",", (1,2,3)) . ")\n";'
select id from table where id in (1,2,3)
$ perl -e'print "select id from table where id in (" . join(",", ()) . ")\n";'
select id from table where id in ()


En alternativ (og vist ækvivalent) måde er:

$ perl -e'print "select id from table where " . join(" OR ", map { "id = " . $_} (1,2,3)) . "\n";'
select id from table where id = 1 OR id = 2 OR id = 3

$ perl -e'print "select id from table where " . join(" OR ", map { "id = " . $_} ()) . "\n";'
select id from table where 

Hvilket også giver en invalid SQL-query ved det tomme array. 

Jesper
-- 
./Jesper Krogh, sslug@sslug, Jabber ID: sslug@sslug



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2006-02-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *