English 中文(简体)
从三个表格的 MySQL 视图
原标题:MySQL view from three tables

我读过许多文章、书籍、录像等,

这么说吧...

三表:

CREATE TABLE IF NOT EXISTS `feature` (  
  `feature_id` INT NOT NULL AUTO_INCREMENT ,  
  `title` VARCHAR(45) NOT NULL ,  
  `description` TEXT NOT NULL ,  
  PRIMARY KEY (`feature_id`) )  
ENGINE = InnoDB;  

CREATE TABLE IF NOT EXISTS `requirement` (  
  `requirement_id` INT NOT NULL AUTO_INCREMENT ,  
  `title` VARCHAR(45) NOT NULL ,  
  `description` TEXT NOT NULL ,  
  PRIMARY KEY (`requirement_id`) ,  
  UNIQUE INDEX `title_UNIQUE` (`title` ASC) )  
ENGINE = InnoDB;  

CREATE TABLE IF NOT EXISTS `feature_requirement` (  
  `fk_feature_id` INT NOT NULL ,  
  `fk_requirement_id` INT NOT NULL ,  
  PRIMARY KEY (`fk_feature_id`, `fk_requirement_id`) ,  
  INDEX `fk_feature_requirement_requirement1` (`fk_requirement_id` ASC) ,  
  INDEX `fk_feature_requirement_feature1` (`fk_feature_id` ASC) )  
ENGINE = InnoDB;  

有些特征可能有多重要求,有些特征也有相同的要求(即对多个特征也有同样的要求),因此,许多至可能的关系的第三个表格。

我希望能够选择特定特征的所有要求(按特性_id),

在很多关于“未知的柱子”和“非独特的别名”的错误信息之后,

CREATE VIEW `project_feature_requirement` AS  
SELECT r.*  
FROM `requirement` `r`  
INNER JOIN `feature_requirement` `fr`  
    on `r`.`requirement_id` = `fr`.`fk_requirement_id`  
INNER JOIN `feature` `f`  
    on `fr`.`fk_feature_id` = `f`.`feature_id`  

However, that only gives me requirement_id, title, & description to SELECT from though. ...which is useless

我还有另外四个想法要创造 同样的查询类型。

任何帮助都将不胜感激!

是否有免费程序可以查询我的计划, 并通过“ Wizard ” 或其它方法将我带入“ wizard ”, 以具体说明我要哪个表格/ 栏目, 以及我想选择哪个栏目, 并为我构建查询??? 或许我终于可以找到它, 如果可以用这样的工具来玩的话 。

Thanks again! Todd

最佳回答

这将对您有效, 但您应该在数据库中进行命名常规工作 。 在同一查询中包含多个域名标题可能会引起不必要的混淆 :

CREATE VIEW `project_feature_requirement` AS  
    SELECT r.*, f.feature_id, f.title as ftitle, f.description as fdesc  
    FROM `requirement` `r`  
    INNER JOIN `feature_requirement` `fr`  
        on `r`.`requirement_id` = `fr`.`fk_requirement_id`  
    INNER JOIN `feature` `f`  
        on `fr`.`fk_feature_id` = `f`.`feature_id`

以更清楚、更不易出错的方式撰写查询:

SELECT r.*, f.feature_id, f.title as ftitle, f.description as fdesc  
FROM feature_requirement fr 
JOIN requirement r ON r.requirement_id=fr.fk_requirement_id
JOIN feature f on f.feature_id=fr.fk_feature_id
问题回答

暂无回答




相关问题
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 ...

热门标签