sql >> データベース >  >> RDS >> Mysql

mysql-各IDの最新の入力

    さて、最終的に私は解決策を見つけました。SQLクエリの最後にこのコードを追加するだけです:

    AND PP.PROFISSIONAL_PORCENTAGEM_Pk = (
    SELECT t2.PROFISSIONAL_PORCENTAGEM_Pk FROM profissional_porcentagem as t2
        where t2.PROFISSIONAL_PORCENTAGEM_FK_CLINICA = PP.PROFISSIONAL_PORCENTAGEM_FK_CLINICA 
        order by t2.PROFISSIONAL_PORCENTAGEM_Pk desc limit 1
    )
    

    次に、クエリ全体を次のようにします。

    SELECT
    PP.PROFISSIONAL_PORCENTAGEM_Pk,
    UST.USER_NAME, 
    ADDR.ADDRESS_NOME, 
    ADDR.ADDRESS_ATIVO,
    PA.PROFESSIONAL_ADDRSS_PK, 
    PP.PROFISSIONAL_PORCENTAGEM_PORCENT,
    PP.PROFISSIONAL_PORCENTAGEM_FK_CLINICA,
    PP.PROFISSIONAL_PORCENTAGEM_DATA
    
    FROM 
    profissional_porcentagem AS PP
    RIGHT JOIN professional_addrss AS PA
        ON(PP.PROFISSIONAL_PORCENTAGEM_FK_CLINICA=PA.PROFESSIONAL_ADDRSS_PK)
    
    INNER JOIN address ADDR
        ON(PA.PROFESSIONAL_ADDRSS_FKADDRSS=ADDR.ADDRESS_Pk)
    
    INNER JOIN professional PF
        ON(PA.PROFESSIONAL_ADDRSS_FKPROFESSIONAL=PF.PROFESSIONAL_Pk)
    
    INNER JOIN usert AS UST
        ON(PF.PROFESSIONAL_FKUSER=UST.USER_Pk)
    
    WHERE UST.USER_Pk=3
    AND ADDR.ADDRESS_ATIVO=1
    
    AND PP.PROFISSIONAL_PORCENTAGEM_Pk = (
    SELECT t2.PROFISSIONAL_PORCENTAGEM_Pk FROM profissional_porcentagem as t2
        where t2.PROFISSIONAL_PORCENTAGEM_FK_CLINICA =   PP.PROFISSIONAL_PORCENTAGEM_FK_CLINICA 
        order by t2.PROFISSIONAL_PORCENTAGEM_Pk desc limit 1
    );
    

    注:私はこの投稿に触発されました Selectユーザーごとの最新の日付の行




    1. カーソル廃止ダンプ

    2. MySQL外部キーONDELETECASCADE3つのテーブル間

    3. .NETのMath.Maxのように2つの値を取るSQLServerのMax関数はありますか?

    4. IDをモーダルボックスに渡してユーザーにメッセージを送信する