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

3
我希望在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并查看结果:

例如:插入用户ID:2(玛丽)

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


这个 like_user 列应该显示什么? - Manoj Salvi
@ManojSalvi,比如LINE应用程序。例如,当用户点击“喜欢”按钮时,该用户每天都会看到点亮的“喜欢”按钮。你能帮我吗? - ydstsh
你需要查询对吧? - Darshan Dave
@darshandave,是的,我需要真实的查询。 - ydstsh
你需要一个WHERE子句,否则你将得到所有的结果。WHERE tbl_user.id_user = 2。 - Puya Sarmidani
@PuyaSarmidani,比如LINE应用程序。例如,当用户点击“喜欢”按钮时,该按钮会为该用户每天点亮。你能帮我吗? - ydstsh
2个回答

2

在选择列中添加以下内容:

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

我还没有测试过它。试一下,也许你应该在这个案例中更改列名。


你的名字像伊朗人的名字,你会说波斯语吗? - ydstsh
但这能帮助您更进一步吗?我没有相同的数据库建立,所以我无法先进行测试。但我的理解是,您想创建一个不存在的列,该列能够显示某人是否喜欢用户。 - Puya Sarmidani
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - ydstsh

0

试试这个查询

    SELECT distinct tbl_content.id_content ,tbl_content.message,tbl_content.ContentLike,tbl_category.name,tbl_user.username,tbl_user_category_content.category
    FROM tbl_user_category_content
    INNER JOIN tbl_user,tbl_cat,tbl_post ON
    tbl_user_category_content.id_user = tbl_user.id_user 
    tbl_user_category_content.id_category = tbl_cat.id_category
    tbl_user_category_content.id_content = tbl_content.id_content
    WHERE tbl_user.id_user='$user_id'

根据您的需求选择列,好的。 - Darshan Dave
谢谢。但是你刚才展示的代码只显示了用户有多少个喜欢,我们也想看到没有被点赞的内容。 - ydstsh
比如LINE应用程序。例如,当用户点击“赞”按钮时,该用户的“赞”按钮每天都会发亮。你能帮我吗? - ydstsh
在用户表中添加一个新列,例如“喜欢/不喜欢”,数据类型为布尔型或MySQL枚举。如果值为1,则显示红色灯,如果为0,则显示白色。明白了吗? - Darshan Dave

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接