php - 如何在MySQL结果中添加字段?

标签 php mysql

我想在 MySQL 的查询结果中添加一个字段。 这个表是我的数据库表:

tbl_user

id_user | username | password
-----------------------------     
  1     |  Pooria    | 123456
  2     |  mary      | 123456
  3     |  july      | 123456
  4     |  Ali       | 123456
  5     |  shahla    | 123456

tbl_category

id_category | name
--------------------
1           | BMW
2           | Toyota
3           | Benz

tbl_content

id_content | message    | ContentLike
--------------------------------------
1          |   Best Car |    2
2          |   Best Car |    3
3          |   Best Car |    4
4          |   Best Car |    1
5          |   Best Car |    1

tbl_user_category_content

id | id_user | id_category | id_content
----------------------------------------
1  |  2      |   2         | 4
1  |  3      |   3         | 3
1  |  4      |   3         | 5
1  |  5      |   1         | 2

tbl_user_like

id_user_like | id_user | id_content
-------------------------------------     
1            |     2   |    5
1            |     2   |    3
1            |     5   |    1
1            |     4   |    2
1            |     4   |    2

我在 Mysql 中的查询:

SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username
        FROM tbl_user_category_content
        INNER JOIN tbl_user ON
        tbl_user_category_content.id_user = tbl_user.id_user 
        INNER JOIN tbl_cat ON
        tbl_user_category_content.id_category = tbl_cat.id_category
        INNER JOIN tbl_post ON
        tbl_user_category_content.id_content = tbl_content.id_content

和结果

id_content | message  | ContentLike | username 
----------------------------------------------
    1      | Best Car | 2           | Pooria
    2      | Best Car | 4           | mary
    3      | Best Car | 3           | july
    4      | Best Car | 5           | Ali
    5      | Best Car | 4           | shahla

我期望结果:

我想插入一个 id_user 并查看结果:

示例:插入 id_user : 2 (mary)

id_content | message       | ContentLike | username    | Like_User 
-------------------------------------------------------------------     
1          | hello world1  | 4           |  Pooria     |  
2          | hello world2  | 2           | mary        |
3          | hello world3  | 2           | july        | Yes
4          | hello world4  | 2           | Ali         |
5          | hello world5  | 2           | shahla      | Yes

添加结果 Like_User

重要提示:我希望数据库中的所有条目都与喜欢的人显示不同的字段。谢谢你帮助我<3

最佳答案

在选择列中添加:

case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User

应该是这样的

SELECT tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username, case when tbl_user.id_user=tbl_user_like.id_user then 'Yes' else 'No' end as Like_User
    FROM tbl_user_category_content
    INNER JOIN tbl_user ON
    tbl_user_category_content.id_user = tbl_user.id_user 
    INNER JOIN tbl_cat ON
    tbl_user_category_content.id_category = tbl_cat.id_category
    INNER JOIN tbl_post ON
    tbl_user_category_content.id_content = tbl_content.id_content

我没有测试过。尝试一下,也许您应该更改案例中的列名

关于php - 如何在MySQL结果中添加字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33473820/

相关文章:

php - mySql - 基于关键字的搜索查询

php - 为什么 php simplexml 允许在这些示例之一中使用 addChild 而在另一个示例中则不允许?

mysql - 在 SQL 中过滤结果

php - Codeigniter 2 多重连接和where语句

php - 如何在 PHP 中设计错误报告

php - Yii2模块未配置! - 选项 : configFile are required error - codeception

php - 使用内连接获取单个值

mysql - 无法在mysql上创建外键

mysql - SQL长查询问题

c# - 根据组合框选择从 MySQL 表中选择值?