使用外键关系的SQL查询

时间:2018-04-29 13:57:22

标签: php mysql sql

我制作了一个图书馆管理系统。我正在尝试打印学生问题书,但我遇到了SQL查询的问题.Below是用于打印问题书的User.php代码。我也在上传数据库以便于参考。请帮忙。

user.php的

<?php
session_start();
$username = $_SESSION['var'];
include ("connection.php");
?>
<html>
<head>
<title>User </title>
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<?php echo "<h2>" . $username . "</h2>";
?>
<table class="w3-table-all w3-card-4">
<tr>
    <th>Book Name </th>
  <th>Issue Date</th>
  <th>Submit Date</th>
</tr>
    <?php
        $query="select isbn, issuedt, submitdt from passbook INNER JOIN usertable on usertable.username='$username' where passbook.id=(usertable.username= '$username') ";
        $result= mysqli_query($sql, $query);
        while($row=$result-> fetch_assoc())
        {
            echo "<tr><td>". $row["isbn"]. "</td><td>". $row["issuedt"]. "</td><td>". $row["submitdt"]."</td><tr>";
        }
        echo "</table>";              
    ?>

</table>
</body>
</html>

以下是图书馆管理系统的数据库。

-- phpMyAdmin SQL Dump
-- version 4.7.9
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 29, 2018 at 03:31 PM
-- Server version: 10.1.31-MariaDB
-- PHP Version: 7.2.3

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `library`
--
CREATE DATABASE IF NOT EXISTS `library` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `library`;

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

--
-- Table structure for table `book`
--

CREATE TABLE `book` (
  `isbn` varchar(13) NOT NULL,
  `bookname` varchar(30) NOT NULL,
  `author` varchar(30) NOT NULL,
  `bookid` int(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- RELATIONSHIPS FOR TABLE `book`:
--   `bookid`
--       `type` -> `bookid_type`
--

--
-- Dumping data for table `book`
--

INSERT INTO `book` (`isbn`, `bookname`, `author`, `bookid`) VALUES
('100', 'harry potter', 'j k rowling', 1);

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

--
-- Table structure for table `passbook`
--

CREATE TABLE `passbook` (
  `pid` int(200) NOT NULL,
  `user_id` int(200) NOT NULL,
  `isbn` varchar(13) NOT NULL,
  `issuedt` date NOT NULL,
  `submitdt` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- RELATIONSHIPS FOR TABLE `passbook`:
--   `user_id`
--       `usertable` -> `id`
--   `isbn`
--       `book` -> `isbn`
--

--
-- Dumping data for table `passbook`
--

INSERT INTO `passbook` (`pid`, `user_id`, `isbn`, `issuedt`, `submitdt`) VALUES
(2, 0, '100', '2018-04-04', '2018-04-26');

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

--
-- Table structure for table `type`
--

 CREATE TABLE `type` (
 `bookid_type` int(30) NOT NULL,
  `name` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
 -- RELATIONSHIPS FOR TABLE `type`:
--

--
-- Dumping data for table `type`
--

INSERT INTO `type` (`bookid_type`, `name`) VALUES
(1, 'Magic');

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

 --
-- Table structure for table `usertable`
--

CREATE TABLE `usertable` (
  `id` int(200) NOT NULL,
  `name` varchar(30) NOT NULL,
  `username` varchar(30) NOT NULL,
  `password` varchar(10) NOT NULL,
  `dateofbirth` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- RELATIONSHIPS FOR TABLE `usertable`:
--

--
-- Dumping data for table `usertable`
--

INSERT INTO `usertable` (`id`, `name`, `username`, `password`, `dateofbirth`) VALUES
(0, 'Priyanka', 'priyanka', 'priyanka', '2018-03-04'),
(1, 'puru', 'puru', 'puru', '2018-03-05');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `book`
--
ALTER TABLE `book`
  ADD PRIMARY KEY (`isbn`),
  ADD UNIQUE KEY `bookid` (`bookid`);

--
-- Indexes for table `passbook`
--
ALTER TABLE `passbook`
  ADD PRIMARY KEY (`pid`),
  ADD UNIQUE KEY `isbn_number` (`isbn`),
  ADD KEY `user_id` (`user_id`);

--
-- Indexes for table `type`
--
ALTER TABLE `type`
  ADD PRIMARY KEY (`bookid_type`);

--
-- Indexes for table `usertable`
--
 ALTER TABLE `usertable`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `passbook`
--
ALTER TABLE `passbook`
  MODIFY `pid` int(200) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `book`
--
ALTER TABLE `book`
  ADD CONSTRAINT `book_ibfk_1` FOREIGN KEY (`bookid`) REFERENCES `type` 
 (`bookid_type`) ON UPDATE CASCADE;

--
-- Constraints for table `passbook`
--
ALTER TABLE `passbook`
  ADD CONSTRAINT `passbook_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES 
`usertable` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `passbook_ibfk_2` FOREIGN KEY (`isbn`) REFERENCES `book` 
(`isbn`) ON DELETE CASCADE;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

数据库以这种方式相关: - usertable具有id作为主键.Book具有isbn作为主键,bookid作为外键。类型具有bookid作为主键。 Passbook将isbn作为外键,uid作为主键。 我想显示存折,其中将显示图书名称以及发布日期和提交日期。因此,我必须从两个不同的表中获取两个数据,使用isbn数字从boo表中获取书名,以及从usertable中获取的用户ID,我必须针对存折表运行以查找相关条目。

1 个答案:

答案 0 :(得分:0)

您的查询应该更像......

$query="select passbook.isbn, issuedt, submitdt, bookname
    from passbook 
    INNER JOIN usertable on usertable.id=passbook.user_id 
    INNER JOIN book on passbook.isbn = book.isbn
    where usertable.username= '$username' ";

这会将存折上的user_id链接到usertable id,并选择要在where子句中限制哪个用户。

添加指向books表的链接意味着将存折isbn链接到book isbn字段。要记住的一件事是isbn然后选择了两个表,所以记得将它添加到所选的字段中(我使用了存折中的字段,尽管它们都是相同的)。

(虽然您应该使用预准备语句来选择bu用户名。)