我有一个表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
相同的用户数据将被多次更新。
那么,我可以使用select * from tracking_table where username="user1"
从所有行中检索该特定用户的所有lans和日志吗?
答案 0 :(得分:1)
是的,你基本上回答了你自己的问题。然而,有一点轻微的复杂性。当您走在正确的轨道上时,您在WHERE子句中使用的逻辑解决方案可能会出现问题,因为您使用的是用户名。现在,您确实指定了“相同”用户。但!当不同的人有相同的名字时你会怎么做?那你就会有问题。最好的方法是拥有一个用户ID。这将是独一无二的,并确保您只能从该用户检索数据。
//You'll have to implement a userId field in your table(s)
SELECT * FROM tracking_table WHERE userId='1';
由于您在标签中添加了PHP,我想知道您以后是否需要在PHP中使用它?
在这种情况下,根据您的语法(mysql_ / mysqli_ / PDO),您可以将它们存储到PHP变量中供以后使用。
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:
<?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'];
?>