繁体   English   中英

从一个条件为的表中检索数据,并在一个SELECT查询中检查另一个表中的记录

[英]Retrieve data from a table with a condition where and check the record in another table in one SELECT query

需要在一个查询查询中从一个条件为“ where”的表中检索数据,同时检查另一个表是否存在ip用户,然后在显示信息的同时显示信息是否存在ip。

表:

Ips (
   Id int (10) not null auto_increment,
   User_id int (10) not null default '0',
   Ip varchar (40) not null
)

Users (
   Id int (11) not null auto_increment,
   Username varchar (35) not null,
   Email varchar (200) not null,
   Salt varchar (40) not null,
   Password varchar (40) not null,
   Country varchar (40) not null
)

我想得到什么:

  1. 从条件为“ where country ='paris'LIMIT 0.10”的用户表中获取数据

  2. 每次您检查ips表是否存在,并带有$ _SERVER ['REMOTE_ADDR']的用户ID和IP地址

  3. 如果表中有记录,ips将在

我当前的问题代码:

<?php

include "connect.php";

$db = mysqli_query($connect, "SELECT * FROM users JOIN ips ON ips.user_id=users.id ON ips.ip='".$_SERVER['REMOTE_ADDR']."' WHERE users.country='paris' LIMIT 0,10");

if (mysqli_num_rows($db)>0){
    while ($data = mysqli_fetch_assoc($db)){
        print_r($data);
        if (filter_var($data['ip'], FILTER_VALIDATE_IP)){ //check var is ip adress
            echo '<br>isset ip';
        }
    }
}else{
    echo 'not records';
}

?>

附言:我在提案中找不到答案

试试这个查询:

SELECT * FROM Users WHERE Country ='paris' AND Id IN (SELECT User_id  FROM Ips  WHERE Ip = 'ip_adddress')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM