Guide d'utilisation MySQL utilisant UNION et LIMIT
P粉680487967
P粉680487967 2023-09-08 12:39:10
0
1
512

Donc, j'ai ce problème à portée de main.

Je dois obtenir plusieurs enregistrements de différentes tables en utilisant la jointure interne et en utilisant UNION ou UNION ALL pour la pagination, le code ci-dessous est utilisé pour obtenir tous les enregistrements et compter. C'est mon ennemi :

(SELECT pp.id as id, pp.*, i.name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
        inner join users u ON pp.users_id $username
        inner join inn_oficial i ON i.photo_id = pp.property_id
        left join plans p ON pp.plans_id = p.id
        WHERE (pp.type = 1 or pp.type = 2) 
        $where ) 
       UNION 
        (SELECT pp.id as id, pp.*, i.model as name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
        inner join users u ON pp.users_id $username
        inner join rent_oficial i ON i.photo_id = pp.property_id
        left join plans p ON pp.plans_id = p.id
        WHERE pp.type = 3
        $where) 
       UNION 
        (SELECT pp.id as id, pp.*, i.name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
        inner join users u ON pp.users_id $username
        inner join fish_pay_oficial i ON i.photo_id = pp.property_id
        left join plans p ON pp.plans_id = p.id
        WHERE pp.type = 4
        $where ) 
       UNION 
        (SELECT pp.id as id, pp.*, i.name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
        inner join users u ON pp.users_id $username
        inner join stores_oficial i ON i.photo_id = pp.property_id
        left join plans p ON pp.plans_id = p.id
        WHERE pp.type = 5
        $where)
       UNION
        (SELECT pp.id as id, pp.*, i.name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
        inner join users u ON pp.users_id $username
        inner join transfer_oficial i ON i.photo_id = pp.property_id
        inner join plans p ON pp.plans_id = p.id
        WHERE pp.type = 6
        $where)
        GROUP BY pp.id
        ORDER BY pp.id

J'ai pris cet exemple de la clause UNION 13.2.9.3 et je l'ai essayé sur un script

Lorsque j'ai essayé d'exécuter le code, il a signalé une erreur

在第1行的'GROUP BY id ORDER BY id LIMIT 0.25'旁边,你的SQL语法有错误

Désolé, mon anglais est horrible, ce n'est pas ma langue maternelle

P粉680487967
P粉680487967

répondre à tous(1)
P粉014293738

J'ai résolu les kskskskks, ajoutez simplement la colonne pp id en tant que id_p, puis triez-la

(SELECT pp.id as id_p, pp.*, i.name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
    inner join users u ON pp.users_id $username
    inner join inn_oficial i ON i.photo_id = pp.property_id
    left join plans p ON pp.plans_id = p.id
    WHERE (pp.type = 1 or pp.type = 2) 
    $where ) 
   UNION 
    (SELECT pp.id as id_p, pp.*, i.model as name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
    inner join users u ON pp.users_id $username
    inner join rent_oficial i ON i.photo_id = pp.property_id
    left join plans p ON pp.plans_id = p.id
    WHERE pp.type = 3
    $where) 
   UNION 
    (SELECT pp.id as id_p, pp.*, i.name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
    inner join users u ON pp.users_id $username
    inner join fish_pay_oficial i ON i.photo_id = pp.property_id
    left join plans p ON pp.plans_id = p.id
    WHERE pp.type = 4
    $where ) 
   UNION 
    (SELECT pp.id as id_p, pp.*, i.name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
    inner join users u ON pp.users_id $username
    inner join stores_oficial i ON i.photo_id = pp.property_id
    left join plans p ON pp.plans_id = p.id
    WHERE pp.type = 5
    $where)
   UNION
    (SELECT pp.id as id_p, pp.*, i.name, p.name as plans_name, u.username,u.email,u.phone, DATE_ADD(pp.init_date, INTERVAL pp.avaliable_days DAY) as end_date FROM property_purchase pp
    inner join users u ON pp.users_id $username
    inner join transfer_oficial i ON i.photo_id = pp.property_id
    inner join plans p ON pp.plans_id = p.id
    WHERE pp.type = 6
    $where)
    ORDER BY id_p $pagination
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal