[英]mysql - Select all from one table and one column form another where $var is found
Ooooookay. Ooooookay。 I have two tables client and users.
我有两个表客户端和用户。 Both have AUTO_INCREMENT id but client table has credid-column whis is foreign key and references to users table's id.
两者都具有AUTO_INCREMENT ID,但客户端表具有可信列,这是外键,并且引用了用户表的ID。
I want to prepare a PHP PDO statement that fetches all columns from client-table and only username-column from users-table where client table's column credid = :var and user table's column id = :var 我想准备一个PHP PDO语句,该语句从客户端表中获取所有列,并且仅从用户表中获取用户名列,其中客户端表的列credid =:var和用户表的列id =:var
So far I have something like this and it is not working 到目前为止,我有这样的事情,它不起作用
$userid = $_SESSION['id']; //echoes a number
$STH = $DBH->prepare("SELECT * FROM client WHERE credid = :id AND username FROM users
WHERE id = :id");
$credentials = array(
':id' => $userid
);
$STH->execute($credentials);
Then like this 然后这样
if ($STH->rowCount() > 0) {
$result = $STH->fetch(PDO::FETCH_ASSOC);
echo $result['columnfoo'];
echo $result['anothercolumn'];
echo etc.
.
.
.
}
This return errors about sql syntax.... 这将返回有关sql语法的错误。
I also tried something like this: 我也尝试过这样的事情:
SELECT client.*,users.username FROM client,users WHERE client.credid =users.id = :id
Returns no errors but not any data either... How should I construct my prepared query? 不返回错误,但也不返回任何数据...我应该如何构造准备好的查询?
You need to use a join
: 您需要使用
join
:
SELECT c.*, u.username
FROM client c
JOIN users u ON u.id = c.credid
WHERE credid = :id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.