简体   繁体   English

如何检查表中是否存在元素(mysql)

[英]How to check if exists element in table (mysql)

I have create a database Mysql and I am using in php.我已经创建了一个数据库 Mysql 并且我在 php 中使用。 I want for each user to be checked if there is data.我希望检查每个用户是否有数据。

I have three tables.我有三张桌子。 In the table User has a unique value called ID_u and isn't auto_increment, the table Application has a unique value called ID(auto_increment) and table InformationUser has a unique value called ID(auto_increment).在表 User 有一个名为 ID_u 的唯一值,而不是 auto_increment,表 Application 有一个名为 ID(auto_increment) 的唯一值,表 InformationUser 有一个名为 ID(auto_increment) 的唯一值。 The tables Users and InformationUser joins with table Application.表 Users 和 InformationUser 与表 Application 连接。

在此处输入图片说明

ID_u = a user with id_u-> 1234

I'am try this:我试试这个:

if( ID_u  exists data){
    location1.php ->data
}else{
    location2.php -> input information
}

I would be thankful for any help!!我将不胜感激!

I'm not sure I understand the structure of your database, but you may take a look at this:我不确定我是否了解您数据库的结构,但您可以看看这个:

$query = "
SELECT * FROM users u
LEFT JOIN application a
ON u.ID_u = a.ID_u
LEFT JOIN informationuser i
ON a.ID = i.ID
//Here you can specify 'WHERE u.ID_u IN (1,2,3,4)'
GROUP BY i.ID
" ;


$users = array() ; //Create storage for users. Use their ID as indeces
$result = $mysqli->query($query) ;
if ($result && $result->num_rows >= 1){
  while ($row = $result->fetch_assoc()){
    if (!isset($users[$row['ID_u'])){
      $users[$row['ID_u']] = array() ; //Create array for a new user
    }
    $users[$row['ID_u']][] = array(  //Fill array in with data and add it to user.
      "email" => $row["email"],
      "phone" => $row["phone"]
    ) ;
  }
}

Afterwards, let's check if data exists for a user with ID = 3 (if 3 is an int ):之后,让我们检查ID = 3的用户的数据是否存在(如果 3 是int ):

if (!empty($users[3])){
  echo "Yeah, data exists for user with ID 3" ;
}

First of all, if the user can only have one set of phone and email, it's better to save that data in the user table so that is has the following structure:首先,如果用户只能拥有一组电话和电子邮件,最好将该数据保存在用户表中,使其具有以下结构:

Users ID_u name surename email phone用户ID_u 姓名确定姓名电子邮件电话

If you are set to keep the structure you have, my first tip is to take away the auto-increment on the table Application.如果您设置保留现有结构,我的第一个提示是取消表应用程序上的自动增量。 That table is just meant to save the correlation between Users and InformationUser.该表只是为了保存用户和信息用户之间的相关性。 Furthermore, the code you're looking for would be something like:此外,您正在寻找的代码类似于:

$sql = "SELECT * FROM Application WHERE ID_u = '1234'";
$result = mysql_query($sql) or die("Woops!");
if ($data = mysql_fetch_array($result)) {
 include("location1.php");
}
else {
 include("location2.php");
}

So you have users and you have applications.所以你有用户,你有应用程序。 On each application there might be a different e-mail and name behind it.每个应用程序背后可能有不同的电子邮件和名称。 So you need to join and see if something exists.所以你需要加入,看看是否有东西存在。 Something of this style这种风格的东西

select count(*)
  from Application 
  join InformationUser on (InformationUser.ID = Application.ID)
 where 
       Application.ID_U = ?

The above gives you a count = 0 if both does not exist.如果两者都不存在,则上述内容为您提供计数 = 0。 If you want to know if both does not exists you can use an outer join.如果您想知道两者是否不存在,您可以使用外连接。

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

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