MySQL在加入时重复

时间:2019-04-25 13:39:04

标签: mysql sql

我正在尝试创建一个聊天应用程序,其中每个对话的引用都存储在称为对话的查找表中,然后从消息表中提取实际的对话,但是我不断在运行此查询时得到重复的数据,有什么想法吗?< / p>

    select t2.text
    from conversations as t1
    inner join messages2 t2
    on t1.thread_id = t2.thread_id 
    where t1.user_id = 1  

//对话表(查找)

    Schema::create('conversations', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('thread_id')->unsigned();            
        $table->integer('user_id')->unsigned();            
        $table->timestamps();
    });

//消息表

    Schema::create('messages2', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('from')->unsigned();
        $table->integer('to')->unsigned();
        $table->integer('thread_id')->unsigned();     
        $table->boolean('read')->default(false);    
        $table->text('text');                                
        $table->timestamps();
    });

1 个答案:

答案 0 :(得分:0)

我不确定您从哪里获得重复数据,但是您应该尝试使用DISTINCT函数

SELECT DISTINCT t2.text
    FROM conversations AS t1
    INNER JOIN messages2 t2
    ON t1.thread_id = t2.thread_id 
    WHERE t1.user_id = 1 

我不确定这是否是您要寻找的东西,但是请继续: https://www.w3schools.com/sql/sql_distinct.asp