简体   繁体   English

在多行中多次添加和检索相同数据

[英]adding and retrieving same data multiple in multiple rows

I have a table tracking_table which will update the user lan and log at regular intervals and the data table will be like below: 我有一个表tracking_table,它将更新用户局域网并定期记录日志,数据表如下所示:

Id  Username    Log     Lan   Timestamp
1   User1      1.555        3.55       12:00PM
2   User2       3.55        4.55       12:10PM
3   User1       6.55    8.66       1:30PM
4   User2       7.88        9.68    2:10PM

The same user data will be updated multiple times. 相同的用户数据将被更新多次。

So, can I use select * from tracking_table where username="user1" to retrieve all the lans and logs of that particular user from all the rows? 那么,我可以使用select * from tracking_table where username="user1"从所有行中检索该特定用户的所有局域网和日志吗?

Yes, you basically answered your own question. 是的,您基本上回答了自己的问题。 However, there's a slight complication. 但是,有一点复杂。 While you were on the right track, your logical solution for what you use in your WHERE clause could be problematic, since you use username. 当您走对路时,由于您使用用户名,因此您在WHERE子句中使用的逻辑解决方案可能会出现问题。 Now, you did specify "same" user. 现在,您确实指定了“相同”用户。 But! 但! What do you do when different people have the same name? 当不同的人具有相同的名字时,您会怎么做? Then you'll have problems. 这样您就会遇到问题。 The best would be to have a user id to go by. 最好的办法是让用户标识通过。 That will be unique and ensure that you will retreive data from that user only. 这将是唯一的,并确保仅从该用户检索数据。

//You'll have to implement a userId field in your table(s)
SELECT * FROM tracking_table WHERE userId='1';

Since you added PHP to your tags, I'm wondering if you need to use it in PHP later? 由于您已将PHP添加到标签中,所以我想知道以后是否需要在PHP中使用它?

In that case, depending on your syntax (mysql_ / mysqli_ / PDO) you can store them into PHP variables for later use. 在这种情况下,根据您的语法(mysql_ / mysqli_ / PDO),您可以将它们存储到PHP变量中以备后用。

mysql_ syntax: mysql_语法:

<?php
//Note that mysql_ is deprecated. I simply just included this in case.
//Select statement
$sql="SELECT * FROM tracking_table WHERE userId='1'";
//Result set from your select
$result=mysql_query($sql);
//The rows of data
$row=mysql_fetch_array($result);

//Row data stored as variables
$id=$row['Id'];
$username=$row['Username'];
$log=$row['Log'];
$lan=$row['Lan'];
$timestamp=$row['Timestamp'];
?>

mysqli_ syntax: mysqli_语法:

<?php
/*
$conn is the database connection variable from you config.php
(assuming that's the name of the include file for your database connection)
*/
//Select statement
$sql="SELECT * FROM tracking_table WHERE userId='1'";
//Result set from your select
$result=mysqli_query($conn, $sql);
//The rows of data
$row=mysql_fetch_array($conn, $result);

//Row data stored as variables
$id=$row['Id'];
$username=$row['Username'];
$log=$row['Log'];
$lan=$row['Lan'];
$timestamp=$row['Timestamp'];
?>

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

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