English 中文(简体)
在MySQL的一次问询中,有两栏与同一表格合并,是否有办法得出结果?
原标题:Is there a way to get a resultset involving 2 columns joined to the same table in one query in MySQL?
  • 时间:2012-01-13 18:52:57
  •  标签:
  • php
  • mysql

我有一个简单的信使系统,我要展示既得信息,又电文。 相关表格/栏目载列如下:

User:

id | firstName | lastName 
-------------------------

Message:

id | fromid | toid | message
----------------------------

I m 座标为:

SELECT message.id, `fromid`, `toid`, `sent`, `message`, message.email, `read`, user.id, `firstName`, `lastName`, `rank`   
FROM message LEFT JOIN `user` ON message.fromid = user.id OR message.toid = user.id  
WHERE `toid` =  $id  OR `fromid` =  $id   
ORDER BY `read` DESC, `sent` DESC";

(为了在我的PHP书写中进行分类,我确实:)

$received = array();  
$sent = array();

while ($dbRow = mysql_fetch_array($query)) {
    if ($dbRow[ toid ] == $id) {
        $received[] = $dbRow;
    } else if ($dbRow[ fromid ] == $id) {
        $sent[] = $dbRow;
    }
}

因此,我想选取以下信息:toid fromid等于用户 sid,然后加以分类(即,如果用户的复制件载于toid)。 该栏从其他用户发送至用户,而如果载于<条码>,则从<>>。 用户一栏将其发送到其他人。

The query almost kind of works, except the $sent[] array contains duplicates: it lists each row twice, once displaying the user s name and the other displays the receiver s name. I can kind of see why, but I don t know what to do to fix it.

我不知道我是否需要调整我的卡片或我的PHP书写,因此我已经把这两条都包括在内。

我知道,我只能把它分成两个问题,但Im试图改进我的工作,并更多地了解日本海洋研究网的工作。

最佳回答

有两个国家加入,即加入“从”到“到”的用户表,另一个时间是“到”的,例如:

SELECT message.id, `fromid`, `toid`, `sent`, `message`, message.email, `read`, fromuser.*, touser.*
FROM message LEFT JOIN `user` fromuser ON message.fromid = user.id 
LEFT JOIN `user` touser ON message.toid = user.id  
WHERE `toid` =  $id  OR `fromid` =  $id   
ORDER BY `read` DESC, `sent` DESC";
问题回答

您是否尝试过<条码>。 看看MySQL想更多地学习。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签