[英]SELECT from two tables in mysql and display all the row's in the column in one of them?
好吧,我對這個有點困惑,我不完全確定如何解釋它。 我會盡力解釋它,我希望你們明白我的意思!
我在mysql中有一個名為members
的表,用於存儲成員數據。 我還有一個稱為transactions
的表,它存儲用戶所做的訂單/事務。
我需要做的是:
1-選擇兩個表。
2-查看兩個表中哪些列具有相同的電子郵件地址(電子郵件保存在兩個表中)。
3-最后使用相同的email
顯示transactions
表中的結果。
我嘗試過類似的東西,但我知道這是錯的:
<?php
// Query member data from the database and ready it for display
$sql = "SELECT payer_email FROM transactions
UNION
SELECT email FROM members";
$query = mysqli_query($db_conx, $sql);
$count = mysqli_num_rows($query);
if ($count > 1) {
echo "There is no user with that id here.";
exit();
}
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$email2 = $sql;
}
?>
有人可以幫我解決這個問題嗎?
編輯:
編輯:
這就是我所做的:請忽略mysql查詢,我會在上線之前將它們轉換為mysqli。 這只是一個測試。
<?php
// Run a select query to get my letest 6 items
// Connect to the MySQL database
include "config/connect_to_mysql.php";
$dynamicList22 = "";
$sql = mysql_query("SELECT
t.payer_email,
t.first_name,
GROUP_CONCAT( t.payer_email )
FROM
transactions t
INNER JOIN
members m
ON( m.email = t.payer_email )
GROUP BY t.payer_email");
$productCount = mysql_num_rows($sql); // count the output amount
while($row = mysql_fetch_array($sql)){
$payer_email = $row["payer_email"];
$dynamicList22 .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="17%" valign="top">'. $payer_email .'</td>
</tr>
</table>';
}
mysql_close();
?>
使用內部聯接作為
select * from `transactions` inner join `members` on `members`.`email` = `transactions`.`email`
嘗試這個...
SELECT
t.payer_email,
t.first_name,
GROUP_CONCAT( t.item_name ) AS item_name
FROM
transactions t
INNER JOIN
members m
ON( m.email = t.payer_email )
GROUP BY t.payer_email
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.