Ich habe eine Tabelle wie folgt aus:Wie kann ich unbeantwortete Fragen auswählen?
CREATE TABLE `qanda` (
`id` int(11) UNSIGNED NOT NULL,
`subject` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`body` text COLLATE utf8_unicode_ci NOT NULL,
`body_html` text COLLATE utf8_unicode_ci NOT NULL,
`related` int(11) UNSIGNED DEFAULT NULL,
`type` tinyint(1) NOT NULL,
`amount` decimal(11,0) DEFAULT NULL,
`closed` tinyint(1) UNSIGNED DEFAULT NULL,
`CloserId` varchar(500) COLLATE utf8_unicode_ci NOT NULL,
`AcceptedAnswer` tinyint(1) DEFAULT NULL,
`aadate` int(11) UNSIGNED DEFAULT NULL,
`category` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
`keywords` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`visibility` tinyint(1) NOT NULL,
`author_id` int(11) UNSIGNED DEFAULT NULL,
`editor_id` int(11) UNSIGNED DEFAULT NULL,
`date_time` int(11) UNSIGNED NOT NULL,
`edited_at` int(11) UNSIGNED DEFAULT NULL,
`activated_at` int(11) UNSIGNED DEFAULT NULL,
`activated_story` enum('سوال شده','ویرایش شده','جواب داده شده','') COLLATE utf8_unicode_ci NOT NULL,
`activator_id` int(11) UNSIGNED DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Diese Tabelle speichert beide Fragen und Antworten. Hier ist meine aktuelle Abfrage, die eine Liste aller Fragen stellt und sortieren sie auf der Grundlage der Abstimmung:
SELECT *, (SELECT COALESCE(sum(vv.value),0)
FROM votes vv
WHERE qanda.id = vv.post_id) AS total_votes
FROM qanda
WHERE type = 0 -- "type=0" means questions
ORDER BY total_votes DESC
LIMIT :j,11;
Jetzt brauche ich eine weitere Bedingung für die WHERE
Klausel Fragen auszuschließen, die Antwort (en) haben. Ich denke, ich brauche eine . Aber ich weiß nicht, wie ich eine join
über die where
Klausel schreiben kann. Gibt es einen Vorschlag?
Es ist sehr seltsam, beide Fragen und Antworten in einer Tabelle zu haben, da dies verschiedene Dinge sind. Sie sollten Ihr Datenmodell ändern und separate Tabellen erstellen. –
Zu Ihrer Frage: Wie sehe ich, auf welche Frage sich eine Antwort bezieht? –
Sie können eine "und nicht existiert (Ihre Abfrage)" in where-Klausel schreiben. wie du Frage ID mit dir hast. –