MySQL从多个表中选择

发布于 2024-12-02 19:09:30 字数 294 浏览 0 评论 0原文

SELECT * FROM dog WHERE (SELECT calluser FROM jos_users WHERE `user_id`='".$cid."')=Subcode

$cidjos_users中的标识符,它告诉我们要获取哪些用户的数据我

要获取的数据在“dog”内,calluser是标识符在两者之间(它告诉我们谁的狗是谁),

我需要能够仅调用与相关用户相关的狗,但它也必须在一个查询中执行。有人可以帮忙吗?非常感谢。

SELECT * FROM dog WHERE (SELECT calluser FROM jos_users WHERE `user_id`='".$cid."')=Subcode

$cid is the identifier in jos_users, which tells us which users we're fetching data about

The data I want to fetch is within "dog", and calluser is the identifier between the two (which tells us who's dogs are who's)

I need to be able to call only the dogs relevant to the user in question, but it also has to be performed in one query. Can anyone help? Much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉栀 2024-12-09 19:09:31

您需要使用联接。
阅读本教程: http://www.tizag.com/mysqlTutorial/

您的查询应该类似于这:

$cid = mysql_real_escape_string($_GET['cid']);
$query = "SELECT d.* FROM dog d
INNER JOIN jos_users ju ON (d.user_id = ju.id) 
WHERE ju.id = '$cid' ";

You need to use joins.
Read this tutorial: http://www.tizag.com/mysqlTutorial/

Your query should look something like this:

$cid = mysql_real_escape_string($_GET['cid']);
$query = "SELECT d.* FROM dog d
INNER JOIN jos_users ju ON (d.user_id = ju.id) 
WHERE ju.id = '$cid' ";
绝對不後悔。 2024-12-09 19:09:31

如果我的理解正确(并且dog表中的id列链接到jos_user表中的calluser列),则查询应该是

SELECT d.* FROM dog AS d JOIN jos_user AS u ON d.id = u.calluser WHERE u.user_id = '$cid'

如果不是,请更详细地解释您的数据结构(可能是小ER图)。

If I got you right (and the id column in the dog-table links to the calluser column in the jos_user-table) the query should be

SELECT d.* FROM dog AS d JOIN jos_user AS u ON d.id = u.calluser WHERE u.user_id = '$cid'

If not please explain your data structure in more detail (maybee small ER diagram).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文