如何在一个查询中组合三个表,我需要所有行?

时间:2013-02-20 05:30:39

标签: php mysql

//( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given

不知道这个问题。请伙计们帮助我。这是我的代码:

$query = "SELECT t1.employeecode, t1.employeename, t2.v, t2.w, t3.total, t3.totals
                FROM invoice t1,salaries t2,table1 t3
                WHERE t1.employeecode = salaries.employeecode AND
                t1.employeecode = t3.employeecode
                ORDER BY t1.employeecode ASC";

    $result = mysql_query($query,$con);

    if(mysql_num_rows($result)>0){

        echo '<table><tr><th>Article title</th>&nbsp;</tr>';
        while($row=mysql_fetch_array($result)){
            //$postedon = strftime("%A %e %b %Y",strtotime($row['postedon']));
            echo '<h1><tr><td><a href="3.php?employeecode='.$row["employeecode"].'">'.$row["deparment"].'</a></td></tr></h1>';
        }

4 个答案:

答案 0 :(得分:2)

$ result是boolean(false)因为查询一定有问题。使用mysql_error()

在查询中收到错误,以帮助自己
$result = mysql_query($query,$con) or die(mysql_error()); 

注意Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated

因此请使用PDOMySQLi(IMO PDO即可)

答案 1 :(得分:2)

由于您将salaries表格别名为t2,因此您必须在where子句中将其引用为t2

$query = "SELECT t1.employeecode, t1.employeename, t2.v, t2.w, t3.total,
            t3.totals
            FROM invoice t1,salaries t2,table1 t3
            WHERE t1.employeecode = t2.employeecode AND
            t2.employeecode = t3.employeecode
            ORDER BY t1.employeecode ASC";

答案 2 :(得分:0)

似乎您的查询无效并且无法返回FALSE bool值。检查在运行时动态构建后需要检查查询的原因 按照以下步骤

$ result = mysql_query($ query)或die($ query。“

”.mysql_error());

检查错误消息。似乎必须包含一些错误的列

答案 3 :(得分:0)

尝试将工资改为t2 ..

WHERE t1.employeecode = salaries.employeecode AND

WHERE t1.employeecode = t2.employeecode AND

我不知道,但是我知道当我们完成给表的别名时,我们的dbms会读取该别名,而不是原生表名。