[英]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: 我想得到什么:
Get data from the users table with the condition "where country = 'paris' LIMIT 0.10" 从条件为“ where country ='paris'LIMIT 0.10”的用户表中获取数据
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地址
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.