首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从两个MYSql表中获取数据并显示为HTML表

从两个MYSql表中获取数据并显示为HTML表
EN

Stack Overflow用户
提问于 2013-01-05 23:40:11
回答 3查看 3.1K关注 0票数 0

我有两个表,第一个是'users‘表,它有一个名为'store’的列,还有一个名为'store‘的表,列有'store number’和‘store location’。

users表中的“store”列是一个“store number”。

我要做的是创建一个HTML表,它类似于

示例数据:

店号: 34店铺位置:伦敦用户: 34

门店号|门店位置|该门店用户数

所以它应该类似于select * from stores和for each create new row。

对于用户数,类似于sum * from users where 'store‘=来自store表的'store number’。

我希望这是有意义的

杰克。

更新:

这是正确的:

代码语言:javascript
复制
$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>";

表:

代码语言:javascript
复制
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 ;

CREATE TABLE IF NOT EXISTS users (

代码语言:javascript
复制
  `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 ;
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-05 23:56:57

试试下面的SQL:

代码语言:javascript
复制
SELECT storenumber, location, COUNT(users.store) as nbr_users FROM stores
LEFT JOIN users ON stores.storenumber = users.store
GROUP BY store.id

然后做

代码语言:javascript
复制
echo $row['nbr_users'];

打印用户数。

票数 1
EN

Stack Overflow用户

发布于 2013-01-06 00:03:32

试试这个:

代码语言:javascript
复制
$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>";
票数 0
EN

Stack Overflow用户

发布于 2013-01-06 00:09:20

你可以试试这个-

代码语言:javascript
复制
$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>";

如果你能提供精确的表格结构,我就能更准确地组织它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14173340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档