Creating a view from multiple tables |
Creating a view from multiple tables |
tudsy |
Aug 5 2018, 11:32 PM
Post
#1
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
Hi
I have 4 tables, CustomersArt,Customers,subscribers and emailcomm. My question is, how do i create a view from these 4 tables with the column email ? I have done a view from one table (see below): $query = "CREATE VIEW ecovib2d AS SELECT email FROM CustomersArt"; Any help will be appreciated. |
CharlesEF |
Aug 21 2018, 02:43 PM
Post
#2
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I don't see anything wrong with the syntax. As I said before, the 4 tables share some column names. Maybe you need to specify column names instead of using the * character and use AS when column names are duplicated. Have you tested using only 1 joined table? Test with 2 tables that don't share any column names. If it works then add another table join to the test. If you start getting errors when column names are duplicated then you know you can't use the * character.
And, I'm not sure 'FULL JOIN' is valid. I don't see it in the MySQL documentation. But, maybe I just missed it. |
tudsy |
Aug 26 2018, 12:48 AM
Post
#3
|
Advanced Member Group: Members Posts: 246 Joined: 30-September 14 Member No.: 21,611 |
I don't see anything wrong with the syntax. As I said before, the 4 tables share some column names. Maybe you need to specify column names instead of using the * character and use AS when column names are duplicated. Have you tested using only 1 joined table? Test with 2 tables that don't share any column names. If it works then add another table join to the test. If you start getting errors when column names are duplicated then you know you can't use the * character. And, I'm not sure 'FULL JOIN' is valid. I don't see it in the MySQL documentation. But, maybe I just missed it. Hi Here is the latest query: $query = "CREATE VIEW ecovib2d AS SELECT Customers.Customersitemid,Customers.email,Customers.name,Customers.Price,Customers.terms,Customers.Age,Customers.Date,Customers.Softbought,CustomersArt.CustomersArtitemid,CustomersArt.email,CustomersArt.graphic,CustomersArt.name,CustomersArt.Price,CustomersArt.Age,CustomersArt.Date FROM Customers join CustomersArt on Customers.Customersitemid = CustomersArt.CustomersArtitemid WHERE email ='$Email'"; but now I get this error: Error Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '='ecovib2d@live.com'' at line 4 Any help will be appreciated. Thanks. |
CharlesEF |
Aug 26 2018, 02:39 AM
Post
#4
|
Programming Fanatic Group: Members Posts: 1,981 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
$query = "CREATE VIEW ecovib2d AS SELECT Customers.Customersitemid,Customers.email,Customers.name,Customers.Price,Customers.terms,Customers.Age,Customers.Date,Customers.Softbought,CustomersArt.CustomersArtitemid,CustomersArt.email,CustomersArt.graphic,CustomersArt.name,CustomersArt.Price,CustomersArt.Age,CustomersArt.Date FROM Customers join CustomersArt on Customers.Customersitemid = CustomersArt.CustomersArtitemid WHERE email ='$Email'"; Based on the column names you posted earlier your query makes no sense. Your not using the same column names. Try this: CODE $query = "CREATE VIEW ecovib2d AS Most of your problems were typos, I think. Also, I didn't include any columns with duplicate names. First see if this query works. If it does then you can try adding columns with the same name. I have already given you an example of this, check post #7. That post uses the column 'name' from 3 tables. Notice the variable name next to each one?SELECT Customers.Itemid, Customers.email, Customers.name, Customers.Price, Customers.terms, Customers.Age, Customers.Date, Customers.Softbought, CustomersArt.Graphic FROM Customers JOIN CustomersArt ON Customers.Itemid = CustomersArt.Itemid WHERE Customers.email ='$Email'"; |
Lo-Fi Version | Time is now: 9th May 2024 - 12:21 PM |