如何选择(所有)表1中的一行,而另一行与表2相同

时间:2018-09-06 21:42:12

标签: php sql sql-server database

我是初学者,我需要完成此SQL语句。因此,我需要知道如何搜索名称,然后在同一表上选择与该名称关联的ID。然后从另一个具有相同ID的表中获取该行。我想要它,因此用户看不到ID,只需要输入名称即可。

示例:

table1       table2
 name       member_id
  id        other-info
 other
 info

会是这样吗?

SELECT table1.name, table1.id, table1.other-info, 
       table2.member_id, table2.other-info
FROM table1 INNER JOIN table2
ON table1.id, table2,member_id

但是,如何输入我的名字?

(尝试过,但是它什么也没做,没有输出) 这是我的connection.php

<?php
$user="root";
$servername = "localhost";
$dbname="test";
$tablename="table1, table2";
$pass="";

$conn= new mysqli($servername,$user,$pass,$dbname);
if (!$conn){
    die("Connection Failed:".mysqli_connect_error());
}
else{
    echo(" Connected to the database ");
}

这是我的display.php

SELECT table1, table2
FROM table1 t1 INNER JOIN table2 t2
ON t1.id= t2.member_id
where t1.name = 'my own example'

1 个答案:

答案 0 :(得分:1)

对于搜索特定名称,您需要将条件放在连接结束的位置,使用表别名是一种好习惯

SELECT t1.name, 
t1.id, 
t1.other-info,
 t2.member_id, t2.other-info
FROM table1 t1 INNER JOIN table2 t2
ON t1.id= t2.member_id
where t1.name = 'Tuhin' //put here your searching name
相关问题