简体   繁体   English

SQL SELECT ID WHERE电子邮件相同

[英]SQL SELECT id WHERE emails are the same

I do not know if it's possible at all, but I have two tables, userBasic and carPlateConfidence , in carPlateConfidence I would like to insert id of userBasic where emails are matched. 我完全不知道是否有可能,但是我有两个表userBasiccarPlateConfidence ,在carPlateConfidence中,我想在匹配电子邮件的地方插入userBasic的ID。

$query .= "INSERT IGNORE INTO userBasic (id_uM, userNameG, userEmailG) values ((SELECT id_uM FROM userMore WHERE userEmailG='$userEmailG'),'$userNameG', '$userEmailG');";

$query .= "INSERT IGNORE INTO carPlateConfidence (emailConfid, id_uB,plateNumber, confidencePlate, plateNumberUn) values ('$userEmailG', (SELECT id_uB FROM userBasic WHERE userEmailG='(SELECT max(emailConfid) FROM carPlateConfidence)'), '$plateNumber','$confidencePlate', '$plateNumberUn');";

So if I have: 所以,如果我有:

userBasic: userBasic:

id_uM = 555;
userNameG = BlaBla;
userEmailG = blabla@blabla.com

And in this table I would like 我想在这张桌子上

carPlateConfidence: carPlate信心:

emailConfid = blabla@blabla.com;
id_uB = 555
plateNumber = 1111
confidencePlate = 70 
plateNumberUn = 2222

AND if email do not matched: 并且,如果电子邮件不匹配:

emailConfid = blabla2@blabla.com;
id_uB = NULL
plateNumber = 1111
confidencePlate = 70
plateNumberUn = 222

P>S> Currently I have tried this, to select id from userBasic: P> S>目前,我已经尝试过从userBasic中选择ID:

(SELECT id_uB FROM userBasic WHERE userEmailG='(SELECT max(emailConfid) FROM carPlateConfidence)')

id_uB in carPlateConfidence is set as foreign key; carPlateConfidence中的id_uB设置为外键;

Tables: 表格:

--
-- Table structure for table `carPlateConfidence`
--

DROP TABLE IF EXISTS `carPlateConfidence`;
CREATE TABLE IF NOT EXISTS `carPlateConfidence` (
  `id_cof` int(11) NOT NULL AUTO_INCREMENT,
  `id_uB` int(11) NOT NULL,
  `emailConfid` varchar(50) NOT NULL,
  `plateNumber` varchar(10) NOT NULL,
  `confidencePlate` varchar(10) DEFAULT NULL,
  `plateNumberUn` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id_cof`),
  KEY `id_uB` (`id_uB`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

-- --------------------------------------------------------

--
-- Table structure for table `userBasic`
--

DROP TABLE IF EXISTS `userBasic`;
CREATE TABLE IF NOT EXISTS `userBasic` (
  `id_uB` int(11) NOT NULL AUTO_INCREMENT,
  `id_uM` int(11) NOT NULL,
  `userNameG` varchar(50) NOT NULL,
  `userEmailG` varchar(50) NOT NULL,
  PRIMARY KEY (`id_uB`),
  UNIQUE KEY `userEmailG` (`userEmailG`),
  KEY `id_uM` (`id_uM`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=119 ;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `carPlateConfidence`
--
ALTER TABLE `carPlateConfidence`
  ADD CONSTRAINT `carPlateConfidence_ibfk_1` FOREIGN KEY (`id_uB`) REFERENCES `userBasic` (`id_uB`);

--
-- Constraints for table `userBasic`
--
ALTER TABLE `userBasic`
  ADD CONSTRAINT `userBasic_ibfk_1` FOREIGN KEY (`id_uM`) REFERENCES `userMore` (`id_uM`);

So you want an update, not an insert : 因此,您需要更新,而不是插入:

UPDATE carPlateConfidence t
SET t.id_uB = (SELECT distinct s.id_uM FROM userBasic s
               WHERE s.userEmailG = t.emailConfid)

This will work only if there can be only 1 match, if there can be more then one match you should specify which one you want, if it doesn't matter, either use MAX() or limit : 仅当只有1个匹配项时才有效,如果有多个匹配项,则应指定所需的匹配项,如果没有关系,请使用MAX()limit

UPDATE carPlateConfidence t
SET t.id_uB = (SELECT max(s.id_uM) FROM userBasic s
               WHERE s.userEmailG = t.emailConfid)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM