连接表,如果该表中的值存在于其他表中,则从表中获取值

t1rydlwq  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(224)

我有两张table。我需要做这项工作,因为我开始为我的游戏开放免费服务。我希望人们能帮忙。
表1“在线”

|ID |
       ---
      | 1 |
      | 2 |
      | 3 |
      | 12|
      | 55|

表2“用户”

UserID|       IP
------------------------
  1   | 123.123.123.123
  2   | 123.123.123.123
  3   | 123.123.123.123
  12   | 123.123.123.123
  55   | 22.12.122.22

第一。我从“online”表中获取值(id)。去拿身份证。
我有一个用户的id是在线的。
下一个。我使用从“online”表中获得的值(id)从“user”表中选择ip地址。
当我从单用户在线获得ip时。我从具有相同“ipaddress”的“user”表中获取userid。它将显示具有相同ip地址的许多用户。
我的问题。如果有那么多用户具有相同的ip和联机,我如何从“联机”表中查询(我的奖金查询)仅联机3个id。
mysql版本不支持select top。所以我不能做这个工作。请帮忙。
谢谢大家!

<?php

session_start();
header('Content-Type: text/html; charset=utf-8');
include ("config.php");
error_reporting(0);
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "data";
$account_Name = $_SESSION['userlogin'];

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// get id online

$id_online = $conn->query("select ID from online");

$online = "";
while($row = $id_online->fetch_assoc())
{
	// list account online

	$online = $row["ID"];
	echo "- ID  online: $online";
	echo "</br>";
	$id_basetab = $conn->query("select UserID, IP from User where UserID='$online'");
	$ip = "";
	//show same ip list
	while($list = $id_basetab->fetch_assoc())
	{
		$ip = $list["LastIP"];
		echo "<hr>- IP: $ip</br>";
		$queryIP = $conn->query("SELECT UserID, IP FROM User where IP=$ip");
		echo "<hr>";
		//echo " acc Clone: </br>";

		while($row1 = $queryIP->fetch_assoc())
		{
				 //echo "id: " . $row1["AccountID"]. " - Name: " . $row1["name"]. " - IP: " . $row1["LastIP"]. "<br>";
		}

	}

	// join table
	echo "<hr>";
	$queryshow = $conn->query("select  UserID, IP from online FULL JOIN User ON UserID=ID where UserID=$online ");
		while($result = $queryshow->fetch_assoc())
		{
       // is my $queryshow correct? I want to get value ID is exist or not if exist UserID on User table.
       // How can I create a query for only 3 users online if they have multiple account online.
		  //I will make my custom query here.

		}
}

?>
0sgqnhkj

0sgqnhkj1#

此查询应一次性获得所需的数据。现在您可以在html和php中重新构造和回显结果。
这将减少很多代码。我使用了内部连接,同时得到了两个表的数据。如果需要where子句,只需将其放在代码的内部联接和ORDERBY节之间。极限3显示数据集中的前3个结果

SELECT User.UserID, User.IP, online.ID 
FROM User 
INNER JOIN online ON User.UserID = online.ID  
ORDER BY User.IP DESC limit 3

相关问题