MySQL 多表选择 WHERE 不比较 2 个表
我有一个查询的开始:
SELECT tf_threads.*, tf_posts.* FROM tf_threads, tf_posts WHERE tf_threads.thread_id=54
当然,这只是一个开始 - 我正在迈出一步随着时间的推移,它会变得更好。然而,我目前所困扰的是如何获取一个 WHERE
子句来将一个表中的列与 PHP 变量而不是另一列进行比较。
我的PHP是这样的:
SELECT $tf_threads.*, $tf_posts.* FROM $tf_threads, $tf_posts WHERE $tf_threads.thread_id=$tf_this_forum
这里重要的一点是 $tf_threads.thread_id =$tf_this_forum
。当我尝试这样做时,它只是不返回任何行,MySQL 说:不可能在读取 const 表后注意到的地方
。当然这应该可以正常工作吗?显然,它没有,所以有人可以帮忙吗?
非常感谢,
詹姆斯
编辑:我也尝试过在纯MySQL中进行查询,如下所示:
SELECT tf_threads.*, tf_posts.* FROM tf_threads, tf_posts WHERE tf_threads.thread_id=54
两个带引号的 54
和不带引号,都具有相同的 no rows 结果。
I have the beginnings of a query:
SELECT tf_threads.*, tf_posts.* FROM tf_threads, tf_posts WHERE tf_threads.thread_id=54
Of course, it's just the start of one - I'm moving one step at a time so it will become much better as time passes. What I'm stuck on at the moment, however, is how to get a WHERE
clause to compare a column in one table with a PHP variable, not another column.
The PHP I have is this:
SELECT $tf_threads.*, $tf_posts.* FROM $tf_threads, $tf_posts WHERE $tf_threads.thread_id=$tf_this_forum
The important bit here is $tf_threads.thread_id=$tf_this_forum
. When I try this it simply returns no rows, with MySQL saying: Impossible WHERE noticed after reading const tables
. Surely this should work fine? Obviously, it doesn't, so can anyone help out here please?
Thanks very much,
James
EDIT: I have also tried doing the query in pure MySQL like this:
SELECT tf_threads.*, tf_posts.* FROM tf_threads, tf_posts WHERE tf_threads.thread_id=54
Both WITH quotes round the 54
and without, with the same no rows result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的查询隐式执行 CROSS JOIN,并且它似乎包含太多
$
符号。我建议进行以下额外更改:
intval
应用于 $tf_this_forum 以确保它是一个整数。$
。关于读取 const 表后注意到的错误 Impossible WHERE 这意味着没有匹配的行。检查您正在搜索的行是否确实存在于数据库中。也可能不是。
Your query is implicitly performing a CROSS JOIN and it appears to contain too many
$
signs.I'd suggest the following extra changes:
intval
applied to $tf_this_forum to ensure that it is an integer.$
when you are referring to a PHP variable.Regarding the error Impossible WHERE noticed after reading const tables this means that no rows matched. Check that the row you are searching for actually exists in your database. It could be that it doesn't.
注意:您应该
mysql_real_escape_string
$tf_this_forum
如果尚未转义。Note: You should
mysql_real_escape_string
$tf_this_forum
if it hasn't yet been escaped.