简体   繁体   English

mysql-从一个表中选择所有内容,并在其中找到$ var的另一列中选择所有内容

[英]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.

相关问题 从一个表中选择所有列,从另一个表中选择一个列,其中列等于变量 - Select all columns from one table and one from another where column equals variable MySQL:如何从一个表中选择和显示所有行,并计算另一个表中where子句的总和? - MySQL: How to select and display ALL rows from one table, and calculate the sum of a where clause on another table? 从一个表中选择列,在另一个表中为NULL - Select column from one table, where it is NULL in another MySQL:仅返回一个表中的行,其中另一个表的一列中的所有值都相同 - MySQL: Return only rows in one table where ALL values in one column of another table are the same MYSQL SELECT从表的所有行中仅选择一列 - MYSQL SELECT only one column from all rows in table 从一张表中选择不在另一张表中 - Select from one table where not in another SQL-是否可以从一列中选择所有不同的值,而另一列中的所有值都匹配? - SQL - Is it possible to select all Distinct Values from one column where all values from another column match? 在mysql中从一个表中选择所有字段,从另一个表中选择一个 - select all field from one table and one from another table in mysql 如何从一个表中获取所有列,而从另一张表中仅获取具有ID的一列? -MySQL - How to get all columns from one table and only one column from another table with ID ? - MySql MySQL查询全选仅找到两个选项之一的地方 - MySQL Query select all where only one of two options are found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM