簡體   English   中英

如何查詢通過外鍵連接的多個表中的數據?

[英]How can I query data from multiple tables connected through foreign keys?

我有一個“ 搜索”頁面 ,用戶可以在其中基於某些條件進行搜索。 我無法正確顯示只顯示所需對象之一的查詢。 它顯示指定條件的多個條目。 這是我們的數據庫設置:

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);

我們做錯了什么? 我們如何獲得適當的搜索結果以顯示?

我不能完全確定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中運行-不確定join語法在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}';";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM