Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi SQL Tags mit und Verknüpfen (https://www.delphipraxis.net/113038-sql-tags-mit-und-verknuepfen.html)

arbu man 1. Mai 2008 17:38

Datenbank: MySQL • Version: 5 • Zugriff über: Zeos / MySQL Connector

SQL Tags mit und Verknüpfen
 
Hi,

ich suche eine passende SQL Abfrage. Ich habe 3 Tabellen(songs: Songdaten Title, Artist; tags: Schlagwörter; songtag: m:n Relation zwischen songs und tags;) Nun möchte ich alle Lieder ausgeben, die die Tags rock, pop und metal haben. Mein Problem liegt dabei in der UND Verknüpfung der Tags.
Für die ODER Abfrage hab ich den SQL-Code:
SQL-Code:
SELECT
   s_artist, s_title
FROM
   music__tagsong ts,
   music__songs s,
   music__tag t
WHERE
   ts.ts_song = s.s_id AND
   ts.ts_tag = t.t_id AND
   t.t_name IN ('rock', 'pop', 'metal')
;
Nur hab ich keine Idee für die UND Abfrage...

Ciao, Björn

mkinzler 1. Mai 2008 17:45

Re: SQL Tags mit und Verknüpfen
 
Poste mal die vollständigen Metadaten der Tabellen

arbu man 1. Mai 2008 18:11

Re: SQL Tags mit und Verknüpfen
 
Ok, ich denke du meinst den Tabellencode:
SQL-Code:
--
-- Tabellenstruktur für Tabelle `music__songs`
--

CREATE TABLE `music__songs` (
  `s_id` int(11) NOT NULL auto_increment,
  `s_title` varchar(255) collate latin1_general_ci NOT NULL,
  `s_artist` varchar(255) collate latin1_general_ci NOT NULL,
  `s_album` varchar(255) collate latin1_general_ci NOT NULL,
  `s_rating` int(11) NOT NULL,
  `s_desc` text collate latin1_general_ci NOT NULL,
  `s_genre` varchar(255) collate latin1_general_ci NOT NULL,
  `s_addtime` int(11) NOT NULL,
  PRIMARY KEY (`s_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=2370 ;

--
-- Tabellenstruktur für Tabelle `music__tag`
--

CREATE TABLE `music__tag` (
  `t_id` int(11) NOT NULL auto_increment,
  `t_name` varchar(255) collate latin1_general_ci NOT NULL,
  `t_count` int(11) NOT NULL,
  PRIMARY KEY (`t_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=1764 ;

--
-- Tabellenstruktur für Tabelle `music__tagsong`
--

CREATE TABLE `music__tagsong` (
  `ts_id` int(11) NOT NULL auto_increment,
  `ts_tag` int(11) NOT NULL,
  `ts_song` int(11) NOT NULL,
  `ts_uid` int(11) NOT NULL default '0',
  PRIMARY KEY (`ts_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=4674 ;

Olli73 1. Mai 2008 18:29

Re: SQL Tags mit und Verknüpfen
 
Meinst du sowas hier?

SQL-Code:
SELECT
   s_artist, s_title
FROM
   music__tagsong ts,
   music__songs s,
   music__tag t
WHERE
   ts.ts_song = s.s_id AND
   ts.ts_tag = t.t_id AND
   t.t_name IN ('rock', 'pop', 'metal')
GROUP BY
   s_artist, s_title
HAVING
   count(*) = 3
;
Oder soll das selbe Lied wirklich 3 mal angezeigt werden?

arbu man 1. Mai 2008 19:08

Re: SQL Tags mit und Verknüpfen
 
Genau das meine ich, die Abfrage funktionier wunderbar Danke :thumb:


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:24 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz