0

Im having problems with constructing a query which filters out posts from table a, that already existing in table b.

Table A:

 `bok_id` int(6) NOT NULL AUTO_INCREMENT,
 `tid` time NOT NULL,
 `datum` date NOT NULL,
 `datum_end` date NOT NULL,
 `framtid` varchar(5) NOT NULL,
 `h_adress` varchar(100) NOT NULL,
 `l_adress` varchar(100) NOT NULL,
 `kund` varchar(100) NOT NULL,
 `typ` varchar(5) NOT NULL,
 `bil` varchar(99) NOT NULL,
 `sign` varchar(2) NOT NULL,
 `tilldelad` varchar(12) NOT NULL,
 `skapad` datetime NOT NULL,
 `endrad` datetime NOT NULL,
 `endrad_av` varchar(2) NOT NULL,
 `kommentar` text NOT NULL,
 `weektype` varchar(3) NOT NULL,
 `Monday` tinyint(1) NOT NULL,
 `Tuesday` tinyint(1) NOT NULL,
 `Wednesday` tinyint(1) NOT NULL,
 `Thursday` tinyint(1) NOT NULL,
 `Friday` tinyint(1) NOT NULL,
 `Saturday` tinyint(1) NOT NULL,
 `Sunday` tinyint(1) NOT NULL,
 `avbokad` varchar(2) NOT NULL,
 `unika_kommentarer` varchar(2) NOT NULL,
 UNIQUE KEY `bok_id` (`bok_id`)

Table B:

 `id` int(6) NOT NULL AUTO_INCREMENT,
 `bok_id` int(6) NOT NULL,
 `tid` time NOT NULL,
 `datum` date NOT NULL,
 `datum_end` date NOT NULL,
 `framtid` varchar(5) NOT NULL,
 `h_adress` varchar(100) NOT NULL,
 `l_adress` varchar(100) NOT NULL,
 `kund` varchar(100) NOT NULL,
 `typ` varchar(5) NOT NULL,
 `bil` varchar(99) NOT NULL,
 `sign` varchar(2) NOT NULL,
 `tilldelad` varchar(12) NOT NULL,
 `skapad` datetime NOT NULL,
 `endrad` datetime NOT NULL,
 `endrad_av` varchar(2) NOT NULL,
 `kommentar` text NOT NULL,
 `weektype` varchar(3) NOT NULL,
 `Monday` tinyint(1) NOT NULL,
 `Tuesday` tinyint(1) NOT NULL,
 `Wednesday` tinyint(1) NOT NULL,
 `Thursday` tinyint(1) NOT NULL,
 `Friday` tinyint(1) NOT NULL,
 `Saturday` tinyint(1) NOT NULL,
 `Sunday` tinyint(1) NOT NULL,
 `avbokad` varchar(2) NOT NULL,
 `unika_kommentarer` varchar(2) NOT NULL,
 UNIQUE KEY `id` (`id`)

What i want is a query which hides all rows in Table A which exists in table B IF Table B.tilldelad=Requested Date. (f.e 2013-09-30)

Im not sure this makes any sense?

What i want is to filter out period bookings as they have been executed and therefore exists in table b which indicates it has been.

Since its recurring events the same bok_id can exists SEVERAL times in table B, but only ONCE in table A...

SELECT * FROM bokningar 
WHERE bokningar.datum <= '2013-09-30' 
AND bokningar.datum_end >= '2013-09-30' 
AND bokningar.typ >= '2' 
AND bokningar.weektype = '1' 
AND bokningar.Monday = '1' ## Monday this is dynamically changed to current date 
AND bokningar.avbokad < '1' 
AND NOT EXISTS ( SELECT 1 FROM tilldelade WHERE tilldelade.tilldelad = '2013-09-30' )

The above code does the trick regarding filtering out rows not in table B, however, if one row in table B has the current date, all results are filtered out. Only rows with corresponding bok_id's is supposted to get filtered out.

Any thoughts on how to do that? Perhaps a Distinct select?

4

3 に答える 3

2

通常、NOT IN 副選択はクエリ時間に非常にコストがかかります。これは通常、LEFT-JOIN を実行し、他のテーブルが NULL のようなものだけを保持することで処理できます...

SELECT * 
   FROM 
      bokningar 
         LEFT JOIN tilldelade 
            on bokningar.bok_id = tilldelade.bok_id
           AND tilldelade.tilldelad = '2013-09-30'
   WHERE 
          bokningar.datum <= '2013-09-30' 
      AND bokningar.datum_end >= '2013-09-30' 
      AND bokningar.typ >= '2' 
      AND bokningar.weektype = '1' 
      AND bokningar.Monday = '1' ## Monday this is dynamically changed to current date 
      AND bokningar.avbokad < '1' 
      AND tilldelade.bok_id IS NULL
于 2013-09-30T14:15:21.357 に答える
0

セット操作を行う必要があります。

ここでは、単純なマイナス クエリで問題を解決できます。

Select * from tableA
minus
Select * from tableB;

これにより、tableB に存在しない tableA のすべてのデータが表示されます。詳細については、こちらをご覧ください。http://en.wikipedia.org/wiki/Set_operations_%28SQL%29

特定のケースでは、これを試してください:

Select * from tableA A
minus
Select * from tableB B where B.tilldelad='3013-09-30';
于 2013-09-30T12:08:49.907 に答える