简体   繁体   English

mysql子查询有多个结果错误

[英]mysql subquery with multiple results error

I have searched for a solution to this problem, some are close but cannot seem to find a solution. 我已经搜索了这个问题的解决方案,有些已经接近但似乎无法找到解决方案。 I have a table of "runs" with various information, one column being information on the company it applies to. 我有一个包含各种信息的“运行”表,一列是关于它适用的公司的信息。 I also have a table of companies storing all the information of the company. 我还有一个公司表,存储公司的所有信息。 In the company column of the runs table, the company is identified by the "indexer" from the company table. 在运行表的公司列中,公司由公司表中的“索引器”标识。

Now i have a search where you can search for any information about the run to find any number of records containing the search phrase, including company. 现在我有一个搜索,您可以在其中搜索有关运行的任何信息,以查找包含搜索短语的任意数量的记录,包括公司。 Being the the company column of the run table just has in identifier number ie. 作为运行表的公司列只有标识符号即。 34, 23, 5 etc i need to also check for matches in the name column of the company table. 34,23,5等我还需要检查公司表名称栏中的匹配项。

This is my query which works fine if only one company matches the search results. 这是我的查询,如果只有一家公司匹配搜索结果,它可以正常工作。 I need to return all company's that match. 我需要退回所有匹配的公司。

$sql_results = "SELECT * FROM runs WHERE name LIKE '%$searchname%' OR company = (SELECT indexer FROM companies WHERE name LIKE '%$searchname%' ) OR feild LIKE '%$searchname%' ORDER BY date_due";

    while($result_results = @mysql_fetch_array($query_results))
    {
    $resultnm_array[]=$result_results['name'];
    $cmp_id = $result_results['company'];
        $sql2 = "SELECT name FROM companies WHERE indexer = '$cmp_id' LIMIT 1";
        $query2 = @mysql_query($sql2);
        $result2 = @mysql_fetch_array($query2);
        $resultcpy_array[]=$result2['name'];
    $resultfld_array[]=$result_results['feild'];
    $resultdt_array[]=$result_results['date_due'];
    $resultid_array[]=$result_results['indexer'];
    }

I am inputting results into an array and using the TBS template engine to create result tables etc. I am a bit of a novice so any help would be appreciated. 我正在将结果输入到数组中并使用TBS模板引擎来创建结果表等。我是一个新手,所以任何帮助将不胜感激。 From what I gather I need to use join. 从我收集的内容中我需要使用join。 But i cant seem to make it work... 但我似乎无法让它工作......

Can you not just use IN : 你能不能只使用IN

SELECT * 
FROM runs 
WHERE name LIKE '%$searchname%' 
    OR company IN (
        SELECT indexer 
        FROM companies 
        WHERE name LIKE '%$searchname%' ) 
    OR feild LIKE '%$searchname%' 
ORDER BY date_due

Given your comments: "works fine if only one company matches the search results" -- that leads me to believe if multiple companies match the search criteria, the query fails because of the = . 鉴于您的意见:“如果只有一家公司匹配搜索结果,则工作正常” - 这使我相信如果多家公司符合搜索条件,则查询因=失败。


Given your edits, you could make this simpler using a JOIN and you'd no longer require the additional select statement in your loop: 鉴于您的编辑,您可以使用JOIN使这更简单,并且您不再需要循环中的其他select语句:

SELECT r.*, c.name
FROM runs r
    JOIN companies c on r.company = c.indexer
WHERE r.name LIKE '%$searchname%' 
    OR r.feild LIKE '%$searchname%' 
ORDER BY r.date_due

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

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