【问题标题】:Join table and get value from table if value in this table is exist on other table如果此表中的值存在于其他表中,则连接表并从表中获取值
【发布时间】:2018-04-10 14:52:01
【问题描述】:

我有 2 张桌子。我需要做这项工作,因为我开始为我的游戏开放免费服务。我希望人们能提供帮助。

表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

首先。我从“在线”表中获取值(ID)。获取身份证件。

我有一个用户的 ID 在线。

接下来。我使用从“在线”表中获得的值(ID)从“用户”表中选择 IP 地址。

当我从单用户在线获得 IP 时。我从具有相同“IPAddress”的“用户”表中获取用户 ID。它将显示许多具有相同 IP 地址的用户。

我的问题。如果有这么多用户具有相同的 IP 和在线,我如何才能从“在线”表中在线查询(我的奖励查询)3 个 ID。

MYSQL 版本不支持选择顶部。所以我不能做这份工作。请帮忙。

谢谢大家!

<?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.
		
		}
}


?>

【问题讨论】:

  • 你会如何选择你想要的三个ID?
  • 既然您正在使用mysqli,为什么不使用prepared statements 来提高您的安全性。
  • 但是你说的top 3 ID online是什么意思? top 的定义是什么?
  • 我只是为自己运行它。用户不会看到它 -¯_(ツ)_/¯
  • 为什么这么多查询都在做这个简单的任务?你可以使用限制

标签: php mysql


【解决方案1】:

此查询应一次性获取您需要的数据。 现在,您可以一次在 html 和 php 中重组和回显结果。
这将减少大量代码。 我使用了内连接并一次获得了两个表的数据。 如果您需要 where 子句,只需将其放在此代码的内部连接和 order by 部分之间。 LIMIT 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多