简体   繁体   English

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

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

Need in one query query retrieve data from one table with condition "where" simultaneously check if another table exists ip user, then display results in while display information whether there exists ip 需要在一个查询查询中从一个条件为“ where”的表中检索数据,同时检查另一个表是否存在ip用户,然后在显示信息的同时显示信息是否存在ip。

tables: 表:

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
)

What I want to get: 我想得到什么:

  1. Get data from the users table with the condition "where country = 'paris' LIMIT 0.10" 从条件为“ where country ='paris'LIMIT 0.10”的用户表中获取数据

  2. Each time you check that the ips table exists with a user id and ip address with $ _SERVER ['REMOTE_ADDR'] 每次您检查ips表是否存在,并带有$ _SERVER ['REMOTE_ADDR']的用户ID和IP地址

  3. If there is a record in the table ips display the information in while 如果表中有记录,ips将在

My current problem code: 我当前的问题代码:

<?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';
}

?>

Ps: I could not find an answer in the proposals 附言:我在提案中找不到答案

试试这个查询:

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.

相关问题 从MySQL表中选择数据,其中来自另一个表的某些结果取决于条件(如果有) - Select data from a MySQL table where some results from another table depends on a condition, if there are any 查询将一条记录(ID)从一张表插入到另一张表 - Query for insert one record (ID) from one table to another table 从一个表中选择行,但语句条件在另一个表中 - Pick rows from one table but where statement condition is in another table Mysql查询选择其中一个表中的总和与另一个表中的行总和进行比较 - Mysql Query select where the sum from one table is compared to the sum of rows in another table 从存在ID(从另一表)的一个表中选择 - Select from one table where id (from another table) exists 使用另一个表上的WHERE子句从表中选择的SQL查询 - SQL Query to select from a table using a WHERE clause on another table 从一个表中检索数据并在mysql中的另一个表中插入/更新 - Retrieve data from one table and insert/update in another table in mysql 如何从表中选择记录,其中记录具有与另一个表最相似的属性 - how to select record from table where record have the most similar attribute from another table 从一个表中选择,并从同一查询中的另一个表计数 - Select from one table, and count from another table in same query MySQL查询通过检查onother表中的其他两个属性从一个表中选择所有数据 - mysql query to select all data from one table by check the other two atributes in onother table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM