Rumah >pangkalan data >tutorial mysql >Mengapa MySQL Melarang Subkueri dalam Klausa FROM View?
"View's SELECT Mengandungi Subquery dalam Klausa FROM": Memahami Kekangan Paparan MySQL
Pandangan MySQL menyediakan cara yang mudah untuk memudahkan pengambilan data dengan membentangkan subset tersuai data daripada jadual asas. Walau bagaimanapun, terdapat sekatan tertentu untuk memastikan integriti dan prestasi pandangan ini.
Salah satu sekatan tersebut ialah larangan subkueri dalam klausa FROM pandangan. Dalam contoh yang disediakan, pertanyaan:
create view view_credit_status as (select credit_orders.client_id, sum(credit_orders.number_of_credits) as purchased, ifnull(t1.credits_used,0) as used from credit_orders left outer join (select * from (select credit_usage.client_id, sum(credits_used) as credits_used from credit_usage group by credit_usage.client_id) as t0 ) as t1 on t1.client_id = credit_orders.client_id where credit_orders.payment_status='Paid' group by credit_orders.client_id)
melemparkan ralat "View's SELECT mengandungi subquery dalam klausa FROM" kerana ia mengandungi subquery berikut:
(select * from (select credit_usage.client_id, sum(credits_used) as credits_used from credit_usage group by credit_usage.client_id) as t0 )
Untuk menyelesaikan masalah ini isu, seseorang boleh membuat pandangan berasingan untuk subkueri. Sebagai contoh, seseorang boleh mencipta paparan berikut:
create view view_credit_orders as (select credit_orders.client_id, sum(credit_orders.number_of_credits) as purchased from credit_orders where credit_orders.payment_status='Paid' group by credit_orders.client_id) create view view_credit_usage as (select credit_usage.client_id, sum(credit_usage.credits_used) as credits_used from credit_usage group by credit_usage.client_id)
Setelah paparan ini dibuat, paparan asal boleh dibuat menggunakan pertanyaan yang diubah suai berikut:
create view view_credit_status as (select view_credit_orders.client_id, view_credit_orders.purchased, ifnull(view_credit_usage.credits_used,0) as used from view_credit_orders left outer join view_credit_usage on view_credit_orders.client_id = view_credit_usage.client_id)
Atas ialah kandungan terperinci Mengapa MySQL Melarang Subkueri dalam Klausa FROM View?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!