简体   繁体   English

从两个不同的mysql表中调用数据

[英]Calling data from two different mysql tables

I am beginner for coding. 我是编码的初学者。

I have a table which I need data from two different mysql database tables. 我有一个表,我需要来自两个不同的mysql数据库表的数据。

Here is so far; 到目前为止,

$query = "SELECT t.*, DATE_FORMAT(t.created, '%e %b %Y') AS payment_date, u.username, u.email FROM prcb_transactions t,  prcb_users u WHERE t.user_id=u.user_id $filter_by ORDER BY $rrorder $rorder LIMIT $from, $results_per_page";
        $result = smart_mysql_query($query);
        $total_on_page = mysql_num_rows($result);

<?php while ($row = mysql_fetch_array($result)) { $cc++; ?>
                  <tr>
                    <td><?php echo $row['username']; ?></td>
                    <td><?php echo $row['title']; ?></td>
                    <td><?php echo $row['transaction_amount']; ?></td>
</tr>
<?php } ?>

The 'title' column is on another table called prcb_retailers but do nt know how to mention for this table in $query. 'title'列位于另一个称为prcb_retailers的表上,但不知道如何在$ query中提及该表。 Can anybody spark my mind? 有人可以激发我的思想吗?

我相信JOINS会为您做到这一点,但是请确保您正确使用了索引。

Maybe with the next query: 也许与下一个查询:

$query="
SELECT
   t.*,
   DATE_FORMAT(t.created, '%e %b %Y') AS payment_date,
   u.username,
   u.email,
   r.*
FROM
   prcb_transactions t,
   prcb_users u,
   prcb_retailers r
WHERE
   r.id=t.retailer_id and
   t.user_id=u.user_id
   $filter_by
ORDER BY
   $rrorder $rorder
LIMIT $from, $results_per_page
";

I guessed the r.id=t.retailer_id part... Probably you have to change the field names to the real ones... 我猜到r.id=t.retailer_id部分...可能您必须将字段名称更改为真实名称...

In order to retrieve the excepted result needs to mention the column 'title' in the selection list. 为了检索例外结果,需要在选择列表中提及“标题”列。 Hope 'Title' field is available in table 'prcb_users' then use column 'u.title' 希望表“ prcb_users”中提供“标题”字段,然后使用“ u.title”列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM