【问题标题】:How can I query data from multiple tables connected through foreign keys?如何从通过外键连接的多个表中查询数据?
【发布时间】:2023-03-20 04:20:02
【问题描述】:

我有一个Search page,用户可以在其中根据特定条件进行搜索。我无法获得仅显示所需对象之一的查询权。它显示指定条件的多个条目。这是我们的数据库设置:

CREATE TABLE Class
    (       
        cid int NOT NULL,
        classNum int,
        classDept varchar(20),
        PRIMARY KEY (cid)
    );

CREATE TABLE Book
    (
        bid int NOT NULL,
        cid int NOT NULL,
        title varchar(20) NOT NULL, 
        author varchar(20) NOT NULL,
        isbn varchar(13),
        price decimal(5,2) NOT NULL,
        PRIMARY KEY (bid),
        FOREIGN KEY (cid) REFERENCES Class (cid)
    );

CREATE TABLE Contact
    (
        contactID int NOT NULL,
        fname varchar(20),
        lname varchar(20),
        contactInfo varchar(90) NOT NULL,
        PRIMARY KEY (contactID)
    );

CREATE TABLE Post
    (
        pid int NOT NULL,
        contactID int NOT NULL,
        bid int NOT NULL,
        postDate date,
        PRIMARY KEY (pid),
        FOREIGN KEY (contactID) references Contact(contactID),
        FOREIGN KEY (bid) references Book(bid)
    );

这是我们用来尝试根据用户输入的条件进行查询的 php 代码。

$author = $_POST["author"];
     $title = $_POST["title"];
     $classNum = $_POST["number"];
     $classDept = $_POST["department"];
     $isbn = $_POST["isbn"];


              $query = "SELECT title, author, isbn, price, classNum, classDept, contactInfo
           FROM Book, Class, Contact, Post
           WHERE Book.cid=Class.cid AND Contact.contactID=Post.ContactID and Book.bid=Post.bid AND Book.author='$author' OR Book.title='$title' ";
     $stid = oci_parse($conn,$query);
     oci_execute($stid,OCI_DEFAULT);

我们做错了什么?我们如何才能显示正确的搜索结果?

【问题讨论】:

  • 请提供一些您认为错误的查询结果数据和样本。还要解释为什么你认为它是错误的。

标签: php html sql oracle


【解决方案1】:

我不完全确定 Oracle 中的连接语法是否与 mysql 相同(这是在其中构建和测试的),但考虑到虚拟数据和对某些表的微小修改(将 auto_increment 添加到主键)它返回与参数匹配的预期两行。

create table if not exists `book` (
  `bid` int(11) not null auto_increment,
  `cid` int(11) not null,
  `title` varchar(64) not null,
  `author` varchar(20) not null,
  `isbn` varchar(13) default null,
  `price` decimal(5,2) not null,
  primary key (`bid`),
  key `cid` (`cid`),
  constraint `book_ibfk_1` foreign key (`cid`) references `class` (`cid`)
) engine=innodb auto_increment=5 default charset=utf8;

insert into `book` (`bid`, `cid`, `title`, `author`, `isbn`, `price`) values
    (1, 1, 'deranged dahlias', 'gertrude geikel', '1234-cdbs-9d', 250.00),
    (2, 1, 'mental magnolias', 'gertrude geikel', '4234-cdfg-9f', 225.00),
    (3, 1, 'raging roses', 'gertrude geikel', '4389-fkpl-8d', 120.25),
    (4, 3, 'peaceful petunias', 'alice cooper', '9043-dflk-01', 500.25);

create table if not exists `class` (
  `cid` int(11) not null auto_increment,
  `classnum` int(11) default null,
  `classdept` varchar(20) default null,
  primary key (`cid`)
) engine=innodb auto_increment=4 default charset=utf8;

insert into `class` (`cid`, `classnum`, `classdept`) values
    (1, 404, 'fookology'),
    (2, 403, 'forbidden fruits'),
    (3, 200, 'goodness');

create table if not exists `contact` (
  `contactid` int(11) not null auto_increment,
  `fname` varchar(20) default null,
  `lname` varchar(20) default null,
  `contactinfo` varchar(90) not null,
  primary key (`contactid`)
) engine=innodb auto_increment=3 default charset=utf8;

insert into `contact` (`contactid`, `fname`, `lname`, `contactinfo`) values
    (1, 'joe', 'bloggs', 'chief headbanger'),
    (2, 'fred', 'flintstone', 'potatopeeler');

create table if not exists `post` (
  `pid` int(11) unsigned not null auto_increment,
  `contactid` int(11) not null,
  `bid` int(11) not null,
  `postdate` date default null,
  primary key (`pid`),
  key `contactid` (`contactid`),
  key `bid` (`bid`),
  constraint `post_ibfk_1` foreign key (`contactid`) references `contact` (`contactid`),
  constraint `post_ibfk_2` foreign key (`bid`) references `book` (`bid`)
) engine=innodb auto_increment=3 default charset=utf8;

insert into `post` (`pid`, `contactid`, `bid`, `postdate`) values
    (1, 1, 1, '2015-12-02'),
    (2, 2, 4, '2015-12-02');

查询在 mysql gui 中运行 - 不确定连接语法在 oracle 中是否相同。

set @_author='alice cooper';
set @_title='mental magnolias';

select * from `book` b
    left outer join `class` c on c.`cid`=b.`cid`
    left outer join `post` p on p.`bid`=b.`bid`
    left outer join `contact` ct on ct.`contactID`=p.`contactID`
    where b.`title`=@_title or b.`author`=@_author;

或者,对于 php

$author=$_POST['author'];
$title=$_POST['title'];

$sql="select * from `book` b
    left outer join `class` c on c.`cid`=b.`cid`
    left outer join `post` p on p.`bid`=b.`bid`
    left outer join `contact` ct on ct.`contactID`=p.`contactID`
    where b.`title`='{$title}' or b.`author`='{$author}';";

【讨论】:

    猜你喜欢
    • 2016-03-06
    • 1970-01-01
    • 2017-05-10
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多