0

私は2つのテーブルを持っています.1つ目は「ストア」という列を持つ「ユーザー」テーブルで、「ストア番号」「ストアの場所」という列を持つ「ストア」というテーブルもあります。

users テーブルの 'store' 列は 'store number' です。

私がやろうとしているのは、次のようなHTMLテーブルを作成することです

サンプルデータ:

店舗数:34 店舗所在地:ロンドン ユーザー数:34

店舗番号 | 店舗所在地 | このストアのユーザー数|

select * from store のようなもので、それぞれに新しい行を作成します。

ユーザー数は sum * from users where 'store' = 'store number' from stores テーブルのようになります。

これが理にかなっているといいのですが、

ジャック。

アップデート:

正解です:

$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 ;
4

3 に答える 3

1

この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'];

ユーザー数を出力します。

于 2013-01-05T15:56:56.820 に答える
0

これを試して:

$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>";
于 2013-01-05T16:03:31.700 に答える
0

あなたはこれを試すことができます-

$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>";

正確なテーブル構造を提供できれば、より正確に組み立てることができます。

于 2013-01-05T16:09:19.983 に答える