Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在单个MySQL查询语句中从表中获取所有记录以及选定的记录_Mysql_Sql_Database - Fatal编程技术网

在单个MySQL查询语句中从表中获取所有记录以及选定的记录

在单个MySQL查询语句中从表中获取所有记录以及选定的记录,mysql,sql,database,Mysql,Sql,Database,我在设计数据库查询方面做得很差,能给我一些帮助吗 以下是两个表格(示例): 现在,我想使用一个查询来获得: 所有relationship\u type表的记录(即SELECT*FROM relationship\u type) 以及具有id=5的用户的特定关系 因此,结果是这样的(假设表关系\u type只有2条记录) 从表1中选择列在columnA=columnB[WHERE other conditions]上连接表2 Oops,它列出了所有内容。当然,从两个表中获取记录是一个简单的查询,可

我在设计数据库查询方面做得很差,能给我一些帮助吗

以下是两个表格(示例):

现在,我想使用一个查询来获得:

  • 所有
    relationship\u type
    表的记录(即
    SELECT*FROM relationship\u type
  • 以及具有
    id=5的用户的特定
    关系
  • 因此,结果是这样的(假设表
    关系\u type
    只有2条记录)


    从表1中选择列在columnA=columnB[WHERE other conditions]上连接表2 Oops,它列出了所有内容。当然,从两个表中获取记录是一个简单的查询,可以设置条件并自定义它。只需使用user.relationship\u type\u id=relationship\u type.relationship\u id where and put条件,数据将被筛选。是否还有其他方法,我也可以列出表relationship\u type中的所有内容?relationship\u type.relationship在列后添加列user.name,用逗号分隔,您将得到所需的列。关闭。还有什么我可以列出表关系类型中的所有内容吗?
    TABLE user: id, name, relationship_type_id
    TABLE relationship_type: relationship_type_id (fk), relationship, contributing
    
    "data": [
            {
                "relationship": "Friend" // for user id = 5
            },
            {
                "relationship_type_id": "1"
                "relationship": "Partner"
            },
            {
                "relationship_type_id": "2"
                "relationship": "Friend"
            }
        ],
    
    SELECT user.id, user.name
    FROM user
    LEFT JOIN relationship_type
    ON user.relationship_type_id=relationship_type.relationship_type_id WHERE user.id=5;
    
    SELECT 
         rt.*, u.*
    FROM 
         relationship_type rt
      LEFT JOIN 
         user u
        ON rt.relationship_type_id = u.relationship_type_id
    WHERE 
         u.id = 5
    UNION
    SELECT *
    FROM relationship_type;