![](/img/trans.png)
[英]Fetch data from two SQL tables with INNER JOIN, display HTML table
[英]Fetch data from two MYSql tables and display as HTML table
我有兩個表,第一個是“用戶”表,其中有一個名為“商店”的列,我也有一個名為“商店”的表,其中有“商店編號”,“商店位置”列。
用戶表中的“商店”列是“商店編號”。
我想做的是創建一個類似HTML表格
樣本數據:
店鋪編號:34店鋪位置:倫敦用戶:34
店鋪編號| 店鋪位置| 該商店的用戶數|
因此,就像從商店中選擇*並為每個創建新行。
對於用戶數量,可以使用諸如sum *來自用戶的數字,其中來自store表的'store'='store number'。
我希望這是有道理的,
插口。
更新:
這是對的:
$result = mysql_query("SELECT * FROM stores", $con) or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Store Number</th> <th>Store Location</th> <th>Number of Users</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['storenumber'];
echo "</td><td>";
echo $row['location'];
echo "</td><td>";
echo 'Amount of users here';
echo "</td></tr>";
}
echo "</table>";
表格:
CREATE TABLE IF NOT EXISTS `stores` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`storenumber` int(11) NOT NULL,
`location` varchar(40) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
如果users
不存在,則創建表(
`id` int(50) NOT NULL AUTO_INCREMENT,
`email` varchar(50) NOT NULL,
`store` int(11) NOT NULL,
`lastvisit` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=28 ;
試試這個SQL:
SELECT storenumber, location, COUNT(users.store) as nbr_users FROM stores
LEFT JOIN users ON stores.storenumber = users.store
GROUP BY store.id
然后做
echo $row['nbr_users'];
打印用戶數。
嘗試這個:
$result = mysql_query("SELECT * FROM stores", $con) or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Store Number</th> <th>Store Location</th> <th>Number of Users</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// gets the total amount of users
$query = mysql_query("SELECT COUNT(*) AS total FROM users WHERE `store`='".$row['storenumber']."'") or die( mysql_error() );
$r = mysql_fetch_array( $query );
$total = $r['total'];
unset($query, $r);
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['storenumber'];
echo "</td><td>";
echo $row['location'];
echo "</td><td>";
echo $total;
echo "</td></tr>";
}
echo "</table>";
你可以試試這個
$result = mysql_query("Select count(user_id) as CNT,storenumber,location from store Left join user ON user.store_number = store.store_number group by store.store_number", $con) or die(mysql_error());
$row = mysql_fetch_assoc($result );
echo "<table border='1'>";
echo "<tr> <th>Store Number</th> <th>Store Location</th> <th>Number of Users</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['storenumber'];
echo "</td><td>";
echo $row['location'];
echo "</td><td>";
echo $row['CNT'];
echo "</td></tr>";
}
echo "</table>";
如果您可以提供准確的表格結構,則可以更准確地進行構圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.