假設我有來自“testsuite”和“fa”的列“id”。如何使用下面的代碼行查詢“fa”并加入“testsuite”?謝謝。
$query = sprintf("select * from testsuite where start between '%s' and '%s' and suite not like '%s' and suite not like '%s'
and serial like '%s'
and ipmimac like '%s'
and testcase like '%s'
order by start desc",
mysql_real_escape_string($date1), mysql_real_escape_string($date2),
mysql_real_escape_string($lab)
);
以下是我嘗試過的。
$query = sprintf("select * from testsuite where start between '%s' and '%s' and suite not like '%s' and suite not like '%s'
and serial like '%s'
and ipmimac like '%s'
and testcase like '%s'
order by start desc",
mysql_real_escape_string($date1), mysql_real_escape_string($date2),
mysql_real_escape_string($lab),
fa.fa as fa.fa_comment
FROM fa
LEFT JOIN fa.id ON testsuite.fa
);
uj5u.com熱心網友回復:
您在變數之后繼續 SQL 陳述句,因此它不是查詢字串的一部分。此外,JOIN 語法不正確。你可以嘗試類似的東西
"select testsuite.*, fa.fa as fa_comment from testsuite
left join fa ON fa.id = testsuite.fa
where start between '%s' and '%s' and suite not like '%s' and suite not like '%s'
and serial like '%s'
and ipmimac like '%s'
and testcase like '%s'
order by start desc"
此外,您還沒有將足夠的變數傳遞給 sprintf。他們需要匹配%s
- 但是我強烈建議不要使用 sprint 和 mysql_real_escape_string,而是依賴使用 PHP 中可用的 mysqli 或 PDO 包的準備好的陳述句。
uj5u.com熱心網友回復:
您必須將連接放在 SQL 字串中,而不是sprintf
.
$query = sprintf("
select *
from testsuite AS ts
LEFT JOIN fa ON fa.id = ts.id
where start between '%s' and '%s'
and suite not like '%s'
and serial like '%s'
and ipmimac like '%s'
and testcase like '%s'
order by start desc",
mysql_real_escape_string($date1), mysql_real_escape_string($date2),
mysql_real_escape_string($lab)
);
uj5u.com熱心網友回復:
我可以在這里看到很多問題。我不知道我從哪里開始...
但是,我認為您搜索類似的內容:
$query = sprintf("
SELECT
testsuite.*,
fa.fa,
fa.fa_comment
FROM testsuite
LEFT JOIN fa
ON fa.id = testsuite.fa
WHERE
testsuite.start BETWEEN '%s' AND '%s'
AND testsuite.suite NOT LIKE '%s'
AND testsuite.suite NOT LIKE '%s'
AND testsuite.serial LIKE '%s'
AND testsuite.ipmimac LIKE '%s'
AND testsuite.testcase LIKE '%s'
ORDER BY
testsuite.start DESC",
mysql_real_escape_string($date1), mysql_real_escape_string($date2),
mysql_real_escape_string($lab)
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/529779.html
標籤:phpsql
下一篇:SQL:在丑陋的資料中展平多行