Creating a view from multiple tables |
Creating a view from multiple tables |
CharlesEF |
Aug 26 2018, 02:39 AM
Post
#21
|
Programming Fanatic Group: Members Posts: 1,984 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'"; |
tudsy |
Sep 4 2018, 11:49 PM
Post
#22
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
Hi
Thanks for that. I renamed the column names with different names. I have now decided to go with a simple select statement. $query ="SELECT Customers.Customersitemid,Customers.email,Customers.name,Customers.Price,Customers.terms,Customers.Age,Customers.Date,Customers.Softbought,CustomersArt.CustomersArtitemid,CustomersArt.nameof,CustomersArt.Priceofart,CustomersArt.Dateof,CustomersArt.termsof,CustomersArt.Ageof,CustomersArt.graphic,CustomersArt.emailadd FROM Customers JOIN CustomersArt ON Customers.Customersitemid = CustomersArt.CustomersArtitemid"; But its not working. I cant see what is the problem. Any help will be appreciated. Thanks. |
CharlesEF |
Sep 5 2018, 12:11 AM
Post
#23
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
$query ="SELECT Customers.Customersitemid, Customers.email, Customers.name, Customers.Price, Customers.terms, Customers.Age, Customers.Date, Customers.Softbought, CustomersArt.CustomersArtitemid, CustomersArt.nameof, CustomersArt.Priceofart ,CustomersArt.Dateof, CustomersArt.termsof, CustomersArt.Ageof, CustomersArt.graphic, CustomersArt.emailadd FROM Customers JOIN CustomersArt ON Customers.Customersitemid = CustomersArt.CustomersArtitemid"; Or, does table 'Customers' have a column named 'Itemid' and does table 'CustomersArt' have a column named 'Itemid'? Which is correct? |
tudsy |
Sep 5 2018, 12:15 AM
Post
#24
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
$query ="SELECT Customers.Customersitemid, Customers.email, Customers.name, Customers.Price, Customers.terms, Customers.Age, Customers.Date, Customers.Softbought, CustomersArt.CustomersArtitemid, CustomersArt.nameof, CustomersArt.Priceofart ,CustomersArt.Dateof, CustomersArt.termsof, CustomersArt.Ageof, CustomersArt.graphic, CustomersArt.emailadd FROM Customers JOIN CustomersArt ON Customers.Customersitemid = CustomersArt.CustomersArtitemid"; Or, does table 'Customers' have a column named 'Itemid' and does table 'CustomersArt' have a column named 'Itemid'? Which is correct? The former. |
CharlesEF |
Sep 6 2018, 12:24 AM
Post
#25
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Create a new PHP script and place this code in it.
CODE <?php Be sure to put your database login information in. Let me know the results of this test.$conn = new mysqli("host", "user", "pass", "dbname"); if($conn->connect_error) { die("Can't connect to database: " . $conn->error); exit(); } $sql = "SELECT b.CustomersArtitemid, b.nameof, b.Priceofart, b.Dateof, b.termsof, b.Ageof, b.graphic, b.emailadd, a.Customersitemid, a.email, a.name, a.Price, a.terms, a.Age, a.Date, a.Softbought FROM Customers a INNER JOIN CustomersArt b ON a.Customersitemid = b.CustomersArtitemid"; if(!$result = $conn->query($sql)) { die("Database Query Error: {$conn->error}"); exit(); } $rows = mysqli_fetch_all($result); var_dump($rows); $result->close(); $conn->close(); ?> |
tudsy |
Sep 6 2018, 09:54 PM
Post
#26
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
Create a new PHP script and place this code in it. CODE <?php Be sure to put your database login information in. Let me know the results of this test.$conn = new mysqli("host", "user", "pass", "dbname"); if($conn->connect_error) { die("Can't connect to database: " . $conn->error); exit(); } $sql = "SELECT b.CustomersArtitemid, b.nameof, b.Priceofart, b.Dateof, b.termsof, b.Ageof, b.graphic, b.emailadd, a.Customersitemid, a.email, a.name, a.Price, a.terms, a.Age, a.Date, a.Softbought FROM Customers a INNER JOIN CustomersArt b ON a.Customersitemid = b.CustomersArtitemid"; if(!$result = $conn->query($sql)) { die("Database Query Error: {$conn->error}"); exit(); } $rows = mysqli_fetch_all($result); var_dump($rows); $result->close(); $conn->close(); ?> Hi Thanks for that. The test produced this: NULL |
CharlesEF |
Sep 6 2018, 10:26 PM
Post
#27
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
That tells me the script did run but returned no results. There might be a logic error in the query. I don't have access to your database so you need to double check that each table/column contains what you think. Example: Are you sure 'Customers.Customersitemid' is equal to 'CustomersArt.CustomersArtitemid'?
This post has been edited by CharlesEF: Sep 6 2018, 10:31 PM |
tudsy |
Sep 6 2018, 10:59 PM
Post
#28
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
That tells me the script did run but returned no results. There might be a logic error in the query. I don't have access to your database so you need to double check that each table/column contains what you think. Example: Are you sure 'Customers.Customersitemid' is equal to 'CustomersArt.CustomersArtitemid'? Hi Thanks for that. Here are the contents of the Customers and CustomersArt tables. Thanks. |
tudsy |
Sep 6 2018, 10:59 PM
Post
#29
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
That tells me the script did run but returned no results. There might be a logic error in the query. I don't have access to your database so you need to double check that each table/column contains what you think. Example: Are you sure 'Customers.Customersitemid' is equal to 'CustomersArt.CustomersArtitemid'? Hi Thanks for that. Here are the contents of the Customers and CustomersArt tables. Thanks. Attached File(s) test.zip ( 297.83k ) Number of downloads: 1093 |
CharlesEF |
Sep 7 2018, 12:40 AM
Post
#30
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
That doesn't help much. Customers contains no data and CustomersArt contains 1 row. Since there is no matching row in Customers nothing is returned.
Let me try to explain what the query does. Customers is considered the header table while CustomersArt is considered the detail table. The query will return all rows in the header table. When a matching row is found in the detail table then the detail row data is returned along with the header row data. Since the table Customers is empty no data is returned. Exactly what do you want the query to do? This post has been edited by CharlesEF: Sep 7 2018, 01:06 AM |
tudsy |
Sep 7 2018, 01:05 AM
Post
#31
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
That doesn't help much. Customers contains no data and CustomersArt contains 1 row. Since there is no matching row in Customers nothing is returned. Exactly what do you want the query to do? Thanks for that. I want the visitor to look at his/her data (the whole database) using his/her email address. |
CharlesEF |
Sep 7 2018, 01:48 AM
Post
#32
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I want the visitor to look at his/her data (the whole database) using his/her email address. |
CharlesEF |
Sep 7 2018, 05:40 PM
Post
#33
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
I want to add that if you are happy with your database layout and just want to search all 4 tables for a certain email address then what you need is UNION, not JOIN. Here is another test script, be sure to change the '$email' value and database login information. Let me know what happens when you run the script.
CODE $email = "xxxxx@yyyyyy.zzz"; $conn = new mysqli("host", "user", "pass", "dbname"); if($conn->connect_error) { die("Can't connect to database: {$conn->error}"); exit(); } $sql = "(SELECT * FROM Customers WHERE `email` = '$email') UNION (SELECT * FROM CustomersArt WHERE `emailadd` = '$email')"; if(!$result = $conn->query($sql)) { die("Database Query Error: {$conn->error}"); exit(); } $rows = mysqli_fetch_all($result); var_dump($rows); $result->close(); $conn->close(); |
tudsy |
Sep 7 2018, 08:50 PM
Post
#34
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
I want to add that if you are happy with your database layout and just want to search all 4 tables for a certain email address then what you need is UNION, not JOIN. Here is another test script, be sure to change the '$email' value and database login information. Let me know what happens when you run the script. CODE $email = "xxxxx@yyyyyy.zzz"; $conn = new mysqli("host", "user", "pass", "dbname"); if($conn->connect_error) { die("Can't connect to database: {$conn->error}"); exit(); } $sql = "(SELECT * FROM Customers WHERE `email` = '$email') UNION (SELECT * FROM CustomersArt WHERE `emailadd` = '$email')"; if(!$result = $conn->query($sql)) { die("Database Query Error: {$conn->error}"); exit(); } $rows = mysqli_fetch_all($result); var_dump($rows); $result->close(); $conn->close(); Results: Viewing your data from ECOVIB2DS database with email address ecovib2d@live.com ********DATA************** 0 ecovib2d@live.com 10_02_am_23_06_05.jpg fdeasv 2 on 67 2018-08-25 00:00:00 |
CharlesEF |
Sep 7 2018, 09:08 PM
Post
#35
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Results: Great, that is what should be returned, based on the contents of the database you posted before.Viewing your data from ECOVIB2DS database with email address ecovib2d@live.com ********DATA************** 0 ecovib2d@live.com 10_02_am_23_06_05.jpg fdeasv 2 on 67 2018-08-25 00:00:00 Now, add the 3rd table to the query and run the script again. If that works then add the 4th table to the query. Remember, you may run in to problems if tables contain the same column name. |
tudsy |
Sep 7 2018, 10:38 PM
Post
#36
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
Results: Great, that is what should be returned, based on the contents of the database you posted before.Viewing your data from ECOVIB2DS database with email address ecovib2d@live.com ********DATA************** 0 ecovib2d@live.com 10_02_am_23_06_05.jpg fdeasv 2 on 67 2018-08-25 00:00:00 Now, add the 3rd table to the query and run the script again. If that works then add the 4th table to the query. Remember, you may run in to problems if tables contain the same column name. Thanks for that. When i add emailcomm I get this error: Database Query Error: The used SELECT statements have a different number of columns Can I use a JOIN to access the data from the tables emailcomm and subscribers?. Currently they have no data in them. When i add a third table it produces the data I want. Thanks. |
tudsy |
Sep 7 2018, 10:47 PM
Post
#37
|
Advanced Member Group: Members Posts: 248 Joined: 30-September 14 Member No.: 21,611 |
Results: Great, that is what should be returned, based on the contents of the database you posted before.Viewing your data from ECOVIB2DS database with email address ecovib2d@live.com ********DATA************** 0 ecovib2d@live.com 10_02_am_23_06_05.jpg fdeasv 2 on 67 2018-08-25 00:00:00 Now, add the 3rd table to the query and run the script again. If that works then add the 4th table to the query. Remember, you may run in to problems if tables contain the same column name. Thanks for that. When i add emailcomm I get this error: Database Query Error: The used SELECT statements have a different number of columns Can I use a JOIN to access the data from the tables emailcomm and subscribers?. Currently they have no data in them. When i add a third table it produces the data I want. Thanks. Here are the column names for emailcomm and subscribers. emailcommitemid Ageofcommmember nameofcommmember emailaddress Dateofsub subscribersitemid username emailsub ConfirmationCode Confirmation Dateofmember Thanks. |
CharlesEF |
Sep 7 2018, 11:46 PM
Post
#38
|
Programming Fanatic Group: Members Posts: 1,984 Joined: 27-April 13 From: Edinburg, Texas Member No.: 19,088 |
Telling me about an error but now showing the query doesn't help much. The query should look like this:
CODE $sql = "(SELECT * FROM `Customers` WHERE `email` = '$email') If you also need to know where each row comes from then the query can be change to show this.UNION (SELECT * FROM `CustomersArt` WHERE `emailadd` = '$email') UNION (SELECT * FROM `emailcomm` WHERE `emailaddress` = '$email') UNION (SELECT * FROM `subscribers` WHERE `emailsub` = '$email')"; |
Lo-Fi Version | Time is now: 27th September 2024 - 04:55 AM |