You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于users和user_details表的SQL查询及表问题咨询

获取用户信息的SQL查询方案

Hey there! Let's work through your user info query problem using the users and user_details tables you mentioned. Since the two tables are linked via usr_id, we'll use JOIN statements to pull together the data you need. Here are some common scenarios tailored to your setup:

1. 获取所有用户的完整信息(包含无详情记录的用户)

If you want to retrieve every user's basic info—even those who don't have a matching entry in user_details—a LEFT JOIN is the way to go. This ensures no user gets excluded:

SELECT 
    u.usr_id,
    u.name,
    ud.det_id,
    ud.seen
FROM users u
LEFT JOIN user_details ud ON u.usr_id = ud.usr_id;

For users without a user_details record, the det_id and seen columns will just show NULL instead of dropping the user entirely.

2. 仅获取有详情记录的用户信息

If you only care about users who already have entries in user_details, switch to an INNER JOIN. This will only return rows where usr_id matches in both tables:

SELECT 
    u.usr_id,
    u.name,
    ud.det_id,
    ud.seen
FROM users u
INNER JOIN user_details ud ON u.usr_id = ud.usr_id;

3. 针对seen字段的筛选(适配未完整展示的内容)

I noticed the seen field's content isn't fully shown, so I'll cover a few common use cases for this field:

  • If seen is a boolean (tracking whether the user was viewed), filter for viewed users:
    SELECT 
        u.usr_id,
        u.name,
        ud.seen
    FROM users u
    LEFT JOIN user_details ud ON u.usr_id = ud.usr_id
    WHERE ud.seen = TRUE;
    
  • If seen is a datetime (tracking when the user was viewed), filter for users viewed in the last 7 days:
    SELECT 
        u.usr_id,
        u.name,
        ud.seen
    FROM users u
    LEFT JOIN user_details ud ON u.usr_id = ud.usr_id
    WHERE ud.seen >= DATE_SUB(CURDATE(), INTERVAL 7 DAY);
    
  • If seen is a status string (like "viewed" or "unviewed"), filter for a specific status:
    SELECT 
        u.usr_id,
        u.name,
        ud.seen
    FROM users u
    LEFT JOIN user_details ud ON u.usr_id = ud.usr_id
    WHERE ud.seen = 'viewed';
    

If your issue with user_details is something specific (like data inconsistencies, performance issues, or a weird filter you can't get right), just share more details and I'll tweak this further!


内容的提问来源于stack exchange,提问作者George Lemery

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:09:36