English 中文(简体)
MySQL JOIN 两个儿童表的问题
原标题:MySQL JOIN problems with two child tables

我现在学习MySQL JOINs,我想知道的是,我是如何去日本女学生会看两个孩子桌子,一个是父亲。

在本案中,问题是:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE= TRADITIONAL ;

DROP SCHEMA IF EXISTS `test` ;

CREATE SCHEMA IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;

USE `test`;

CREATE  TABLE IF NOT EXISTS `test`.`objetos` (
  `idobjetos` INT(11) NOT NULL AUTO_INCREMENT ,
  `modelo` VARCHAR(45) NULL DEFAULT NULL ,
  `descricao` VARCHAR(45) NULL DEFAULT NULL ,
  `token_dono` VARCHAR(41) NOT NULL ,
  PRIMARY KEY (`idobjetos`, `token_dono`) ,
  INDEX `token_dono` (`token_dono` ASC) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

CREATE  TABLE IF NOT EXISTS `test`.`empresas` (
  `idempresa` INT(11) NULL DEFAULT NULL AUTO_INCREMENT ,
  `nome` VARCHAR(45) NULL DEFAULT NULL ,
  `cnpj` VARCHAR(45) NULL DEFAULT NULL ,
  `email` VARCHAR(45) NULL DEFAULT NULL ,
  `telefone` VARCHAR(45) NULL DEFAULT NULL ,
  `token` VARCHAR(41) NOT NULL ,
  PRIMARY KEY (`idempresa`, `token`) ,
  INDEX `token_empresa` (`token` ASC) ,
  UNIQUE INDEX `cnpj_UNIQUE` (`cnpj` ASC) ,
  UNIQUE INDEX `email_UNIQUE` (`email` ASC) ,
  UNIQUE INDEX `telefone_UNIQUE` (`telefone` ASC) ,
  CONSTRAINT `token_empresa`
    FOREIGN KEY (`token` )
    REFERENCES `test`.`objetos` (`token_dono` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

CREATE  TABLE IF NOT EXISTS `test`.`civis` (
  `idcivil` INT(11) NOT NULL AUTO_INCREMENT ,
  `nome` VARCHAR(45) NULL DEFAULT NULL ,
  `cpf` VARCHAR(45) NULL DEFAULT NULL ,
  `email` VARCHAR(45) NULL DEFAULT NULL ,
  `token` VARCHAR(41) NOT NULL ,
  PRIMARY KEY (`idcivil`, `token`) ,
  INDEX `token_civil` (`token` ASC) ,
  UNIQUE INDEX `cpf_UNIQUE` (`cpf` ASC) ,
  UNIQUE INDEX `email_UNIQUE` (`email` ASC) ,
  CONSTRAINT `token_civil`
    FOREIGN KEY (`token` )
    REFERENCES `test`.`objetos` (`token_dono` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;

CREATE  TABLE IF NOT EXISTS `test`.`an_users` (
  `id` INT(11) NULL DEFAULT NULL AUTO_INCREMENT ,
  `usuario` VARCHAR(45) NULL DEFAULT NULL ,
  `senha` VARCHAR(45) NULL DEFAULT NULL ,
  `cod_usuario` INT(11) NOT NULL ,
  PRIMARY KEY (`id`, `cod_usuario`) ,
  INDEX `cod_usuario_fk` (`cod_usuario` ASC) ,
  CONSTRAINT `cod_usuario_fk`
    FOREIGN KEY (`cod_usuario` )
    REFERENCES `test`.`empresas` (`idempresa` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

And I ve inserted these values:

INSERT INTO `mydb`.`objetos` VALUES (NULL,  Cadeira XTT ,  Quê? , SHA1(CONCAT(3913123612, [email protected] )));
INSERT INTO `mydb`.`objetos` VALUES (NULL,  Mesa TTX ,  Hein? , SHA1(CONCAT(4313123612, [email protected] )));
INSERT INTO `mydb`.`objetos` VALUES (NULL,  Prédio TT ,  Hein? , SHA1(CONCAT(73358847000116, [email protected] )));

INSERT INTO `mydb`.`civis` VALUES (NULL,  One , 3913123612,  [email protected] , SHA1(CONCAT(3913123612, [email protected] )));
INSERT INTO `mydb`.`civis` VALUES (NULL,  Two , 4313123612,  [email protected] , SHA1(CONCAT(4313123612, [email protected] )));

INSERT INTO `mydb`.`empresas` VALUES(NULL,  Buzz , 73358847000116,  [email protected] , 33270743, SHA1(CONCAT(73358847000116, [email protected] )));

但是,在我希望检索记录的数据时,有一些问题。 北大西洋渔业组织与预期的工作不一样,只吸收有相同症状的浏览器,我可以这样做:

SELECT * 
FROM objetos 

JOIN civis 
ON(objetos.token_dono = civis.token) 

JOIN empresas 
ON(objetos.token_dono = empresas.token)

从数据库中列出所有“objetos”的准确信息。 但是,随着我对它进行测试,它不会奏效。

如果有人能够让我了解这些问题,我将不胜感激。

问题回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签