我正在使用三个SQL表,并且正在使用INNER JOIN联接这些表。这是表格的概述。
CREATE TABLE `User` (
`id` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`firstName` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`lastName` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
CREATE TABLE `Ownership` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`certificate` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`user`,`certificate`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `Certificate` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`domain` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`creationDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`expirationDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`type` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
加入表的请求如下:
select user, lastName, domain
from Ownership
INNER JOIN User ON Ownership.user = User.id
INNER JOIN Certificate ON Ownership.certificate = Certificate.id;`
我想要的是只为一个域名检索一个用户,并创建一个仅在certificate.domain值不存在的情况下才能加入的例外。
MYSQL有可能吗?
谢谢
答案 0 :(得分:1)
您需要进行预查询,以至少限定与给定域关联的某些唯一性组件。为此,请考虑一下。
import re
test_str = "[('15000042', 19)]"
result = re.sub(r"[^\w,]+", "", test_str)
if result:
print (result)
因此,这将获得一个给定域的用户。现在,使用THAT结果获取与该域关联的用户。
select
C.domain,
min( O.user ) JustOneUser
from
Certificate C
JOIN Ownership O
on C.ID = O.Certificate
group by
C.Domain