我遇到了一个情况,我有2个表,一个有id作为PK而另一个有,另一个有时间戳,状态和id作为PK。当table1的id与表2的id匹配时,我想从table1中选择所有唯一的行。
例如:
表1
----- ID ------------时间戳--------------------------状态 - ----------- COL3 ------
..... 111 ............ 2017-10-05 10:42:23 ................. K. .........
..... 111 ............... 2017-10-05 12:42:23 .............. X. .........
..... 111 ............... 2017-10-05 18:42:23 .............. Y. .........
..... 222 ............... 2017-10-05 11:42:23 .............. B. .........
表2
----- ID ------------ COL2 -------------------------- col3- ----------- COL4 ------
..... 111 ........... CCCC ............................ KKKY ..........
..... 222 ............ HSGHXF ......................... OPUB .. ........
这里我想从table1中选择2个唯一的行,其中一个id为111,另一个id为222。
答案 0 :(得分:1)
尝试:
SELECT * FROM table1 AS tbl1 LEFT JOIN table2 AS tbl2 ON tbl1.ID = tbl2.ID
答案 1 :(得分:0)
// I Have two unique rows match and display data in table...
<?php
$con=mysql_connect('localhost','root','')or die("not connect".mysql_error());
$db=mysql_select_db('persons')or die("could Not connect".mysql_error());
$res="select persond.personid,orders.OrderNumber,persond.firstname,persond.lastname from persond LEFT JOIN orders ON persond.personid=orders.PersonID" or die("could Not select".mysql_error());
$query=mysql_query($res);
echo "<table border='1'>";
echo "<th>id</th><th>first Name</th><th>lastname</th><th>ordernumber</th>";
while($row=mysql_fetch_array($query)){
echo "<tr>";
//echo "<td><br/>".$row['id']."</td>";
echo "<td>".$row['personid']."</td>";
echo "<td>".$row['firstname']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['OrderNumber']."</td></tr>";
}
?>