简体   繁体   中英

mysql create view from multiple tables

I want create view from tabel admin, akademik, dosen and mahasiswa, in this view there are have admin, password and id level columns.

have tried this query, but the result is erorr.

CREATE VIEW view_user AS
    SELECT admin.id_level, admin.username, admin.password, akademik.id_level, akademik.username, 
    akademik.password, dosen.id_level, dosen.username, dosen.password, mahasiswa.id_level, 
    mahasiswa.username, mahasiswa.password
FROM admin a
    INNER JOIN akademik b on (a.id_level=b.id_level) AND (a.username=b.username) AND (a.password=b.password)
    INNER JOIN dosen c on (b.id_level=c.id_level) AND (b.username=c.username) AND (b.password=c.password)
    INNER JOIN mahasiswa d on (c.id_level=d.id_level) AND (c.username=d.username) AND (c.password=d.password)
ORDER BY 1;

I want results like this我想要这样的结果

How can I do that? Can somebody help me

Since you are doing a Inner JOIN on id_level, user_name and password (generally not advised) you don't have to display these 3 columns from all 4 tables, as your desired output is only 3.

CREATE VIEW view_user AS
    SELECT admin.id_level, admin.username, admin.password
FROM admin a
    INNER JOIN akademik b on (a.id_level=b.id_level) AND (a.username=b.username) AND (a.password=b.password)
    INNER JOIN dosen c on (b.id_level=c.id_level) AND (b.username=c.username) AND (b.password=c.password)
    INNER JOIN mahasiswa d on (c.id_level=d.id_level) AND (c.username=d.username) AND (c.password=d.password)
ORDER BY 1;

Additionally you can remove the order in view creation and do it when querying your view, like this

Select * from view_user order by 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM