📜  PHP – MySQL:嵌套查询

📅  最后修改于: 2022-05-13 01:55:06.797000             🧑  作者: Mango

PHP – MySQL:嵌套查询

在本文中,我们将使用 Xampp 服务器对 MySQL 服务器中的数据库执行嵌套查询操作。

介绍 :

PHP代表超文本预处理器,它是一种服务器端脚本语言,也用于处理数据库操作。我们是一个PHP xampp 服务器,用于与数据库进行通信。使用的语言是 MySQL。 MySQL 是一种数据库查询语言,用于管理数据库。它通过执行一些操作与PHP通信并管理数据库。

嵌套查询:

它也称为子查询,或者我们可以说在查询中使用的查询称为嵌套查询。我们正在使用 College_details 数据来执行嵌套查询。



句法 :

SELECT column1,column2,column3,...,columnn
FROM table_name
WHERE column_name expression operator  
   ( SELECT COLUMN_NAME ...n from TABLE_NAME   WHERE ... );

(SELECT COLUMN_NAME ...n from TABLE_NAME WHERE ... );是一个内部/子查询。

例子 :

考虑表。

  • 嵌套查询以根据 sid 获取学生的所有详细信息,
SELECT * FROM college1 WHERE sid IN (SELECT sid FROM college1);

结果 :

student id: 1 - student name: sravan kumar - student address: kakumanu
student id: 2 - student name: bobby - student address: kakumanu
student id: 3 - student name: ojaswi - student address: hyderabad
student id: 4 - student name: rohith - student address: hyderabad
student id: 5 - student name: gnanesh - student address: hyderabad
  • 嵌套查询以根据 sid 小于 4 获取学生的所有详细信息
SELECT * FROM college1 WHERE sid IN (SELECT sid where sid < 4);

结果 :

student id: 1 - student name: sravan kumar - student address: kakumanu
student id: 2 - student name: bobby - student address: kakumanu
student id: 3 - student name: ojaswi - student address: hyderabad

方法 :



  • 创建一个数据库并在其中创建一个表。
  • 编写PHP代码向其中插入数据
  • 编写PHP代码来执行嵌套查询。

脚步 :

  • 启动 Xampp 服务器

  • 创建一个名为 gfg 的数据库并创建表 College1

大学1列:

  • 使用PHP代码向表中插入记录
PHP
connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
//insert records into table
$sql  = "INSERT INTO college1 VALUES (1,'sravan kumar','kakumanu');";
$sql .= "INSERT INTO college1 VALUES (2,'bobby','kakumanu');";
$sql .= "INSERT INTO college1 VALUES (3,'ojaswi','hyderabad');";
$sql .= "INSERT INTO college1  VALUES (4,'rohith','hyderabad');";
$sql .= "INSERT INTO college1  VALUES (5,'gnanesh','hyderabad');";
  
if ($conn->multi_query($sql) === TRUE) {
  echo "college 1 data inserted successfully";
} else {
  echo "Error: " . $sql . "
" . $conn->error; }    $conn->close(); ?>


PHP


"; echo "nested query Demo "; echo"";
echo "
"; echo "

";echo "nested query to get student all details based on sid";echo "

"; echo "
"; echo "
"; //sql query $sql = "SELECT  * FROM college1 WHERE sid IN (SELECT sid FROM college1)"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " student id: ". $row['sid']," - student name: ". $row['sname']," -  student address: ". $row['saddress'];            echo "
"; }    echo "
"; echo "

";echo "nested query to get student all details based on sid less than 4";echo "

"; echo "
"; echo "
"; //sql query $sql = "SELECT  * FROM college1 WHERE sid IN (SELECT sid where sid < 4)"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " student id: ". $row['sid']," - student name: ". $row['sname']," -  student address: ". $row['saddress'];            echo "
"; } //close the connection    $conn->close(); ?>


PHP


"; echo "nested query Demo "; echo"";
echo "
"; echo "

";echo "nested query to get student id";echo "

"; echo "
"; echo "
"; //sql query $sql = "SELECT  sid FROM college1 WHERE sid IN (SELECT sid FROM college1)"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " student id: ". $row['sid'];     echo "
"; }    echo "
"; echo "

";echo "nested query to get student name ";echo "

"; echo "
"; echo "
"; //sql query $sql = "SELECT  sname FROM college1 WHERE sid IN (SELECT sid FROM college1)"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " student name: ". $row['sname'];     echo "
"; }    //close the connection    $conn->close(); ?>


输出 :

输入“本地主机/插入。 PHP”在浏览器中运行程序。

插入的数据,



编写PHP代码来执行嵌套查询

形式。 PHP

PHP



"; echo "nested query Demo "; echo"";
echo "
"; echo "

";echo "nested query to get student all details based on sid";echo "

"; echo "
"; echo "
"; //sql query $sql = "SELECT  * FROM college1 WHERE sid IN (SELECT sid FROM college1)"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " student id: ". $row['sid']," - student name: ". $row['sname']," -  student address: ". $row['saddress'];            echo "
"; }    echo "
"; echo "

";echo "nested query to get student all details based on sid less than 4";echo "

"; echo "
"; echo "
"; //sql query $sql = "SELECT  * FROM college1 WHERE sid IN (SELECT sid where sid < 4)"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " student id: ". $row['sid']," - student name: ". $row['sname']," -  student address: ". $row['saddress'];            echo "
"; } //close the connection    $conn->close(); ?>

输出 :

示例 2

形式1。 PHP

PHP



"; echo "nested query Demo "; echo"";
echo "
"; echo "

";echo "nested query to get student id";echo "

"; echo "
"; echo "
"; //sql query $sql = "SELECT  sid FROM college1 WHERE sid IN (SELECT sid FROM college1)"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " student id: ". $row['sid'];     echo "
"; }    echo "
"; echo "

";echo "nested query to get student name ";echo "

"; echo "
"; echo "
"; //sql query $sql = "SELECT  sname FROM college1 WHERE sid IN (SELECT sid FROM college1)"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " student name: ". $row['sname'];     echo "
"; }    //close the connection    $conn->close(); ?>

输出 :