Help - Search - Members - Calendar
Full Version: Creating a view from multiple tables
HTMLHelp Forums > Programming > Databases
tudsy
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
You need to look in to INNER JOIN or OUTER JOIN to link the tables. You don't provide enough detail for me to suggest anything else.
tudsy
QUOTE(CharlesEF @ Aug 7 2018, 04:12 AM) *

You need to look in to INNER JOIN or OUTER JOIN to link the tables. You don't provide enough detail for me to suggest anything else.

Hi

Thanks for that.

More detail.

Here are the tables with the column names:


CustomersArt(Itemid,email,Graphic,name,Price,terms,Age,Date);
Customers(Itemid,email,name,Price,terms,Age,Date,Softbought);
emailcomm(Itemid,Age,name,email,Date);
subscribers(itemid,username,email,ConfirmationCode,Confirmation,Date);

Thanks.
CharlesEF
You still don't tell me what columns you want from each table. Anyway, you should post your own code that shows your attempt and then I can help. You won't learn anything if I write it for you.
tudsy
QUOTE(CharlesEF @ Aug 7 2018, 04:37 AM) *

You still don't tell me what columns you want from each table. Anyway, you should post your own code that shows your attempt and then I can help. You won't learn anything if I write it for you.



Thanks for that.

The user or visitor enters an email address and displays the entire 4 tables according to the email address.

Thanks.

tudsy
Hi

A solution:

“CREATE VIEW ecovib2d AS
SELECT CustomersArt.email,Customers.email,emailcomm.email,subscribers.email”;


Is this correct?

Thanks.
CharlesEF
No. You're not joining any tables. Here is an example, taken from here:
CODE
CREATE VIEW GiftsList
AS
SELECT  b.name user_from,
        c.name user_to,
        d.name gift_name,
        d.price gift_price
FROM    gift a
        INNER JOIN users b
            ON a.user_from = b.id
        INNER JOIN users c
            ON a.user_from = c.id
        INNER JOIN items d
            ON a.item = d.id
CharlesEF
You should start by joining just 1 table. I haven't tried to explain INNER JOIN because there are hundreds of examples on the internet.
CharlesEF
Ok, here is a hint. Since you never said which columns you want I have included all of them from Customers.
CODE
$query = "CREATE VIEW ecovib2d AS SELECT a.email, b.name, b.Price, b.terms, b.Age, b.Date, b.Softbought FROM CustomersArt a
INNER JOIN Customers b ON a.email = b.email";
You still need to add columns from the other 2 tables but this should get you started. You may not need INNER JOIN, maybe a regular JOIN will work?
tudsy
QUOTE(CharlesEF @ Aug 12 2018, 02:51 AM) *

Ok, here is a hint. Since you never said which columns you want I have included all of them from Customers.
CODE
$query = "CREATE VIEW ecovib2d AS SELECT a.email, b.name, b.Price, b.terms, b.Age, b.Date, b.Softbought FROM CustomersArt a
INNER JOIN Customers b ON a.email = b.email";
You still need to add columns from the other 2 tables but this should get you started. You may not need INNER JOIN, maybe a regular JOIN will work?


Thanks for that.

Maybe this would work ?



$query = "CREATE VIEW ecovib2d AS
SELECT
CustomersArt.*,
Customers.*,
emailcomm.*,
subscribers.*
FROM CustomersArt
JOIN Customers
ON Customers.* = CustomersArt.*
JOIN emailcomm
ON emailcomm.* = Customers.*
JOIN subscribers
ON subscribers.* = emailcomm.*
WHERE email = $Email";

Thanks.
CharlesEF
I don't think so, you need to change the ON part. You must join based on the email address. Also, some of your tables have the same column name. You might have to use an alias for those columns (look in to AS). In other words, you might have to specify column names and not use the * character. And you might want to use table names only once. In my post a = table CustomersArt while b = Customers. I've never created a VIEW so you may have to do some testing.
tudsy
QUOTE(CharlesEF @ Aug 12 2018, 02:46 PM) *

I don't think so, you need to change the ON part. You must join based on the email address. Also, some of your tables have the same column name. You might have to use an alias for those columns (look in to AS). In other words, you might have to specify column names and not use the * character. And you might want to use table names only once. In my post a = table CustomersArt while b = Customers. I've never created a VIEW so you may have to do some testing.



Hi

I have been getting this 1064 error (syntax error) on the following query:

CREATE VIEW ecovib2d AS
SELECT CustomersVideo.*,Customers.*
FROM CustomersVideo
FULLJOIN Customers
ON CustomersVideo.email = Customers.email
WHERE email =".'$Email.';

Any help will be appreciated.
tudsy
QUOTE(tudsy @ Aug 15 2018, 10:51 AM) *

QUOTE(CharlesEF @ Aug 12 2018, 02:46 PM) *

I don't think so, you need to change the ON part. You must join based on the email address. Also, some of your tables have the same column name. You might have to use an alias for those columns (look in to AS). In other words, you might have to specify column names and not use the * character. And you might want to use table names only once. In my post a = table CustomersArt while b = Customers. I've never created a VIEW so you may have to do some testing.



Hi

I have been getting this 1064 error (syntax error) on the following query:

CREATE VIEW ecovib2d AS
SELECT CustomersVideo.*,Customers.*
FROM CustomersVideo
FULLJOIN Customers
ON CustomersVideo.email = Customers.email
WHERE email =".'$Email.';



I may have found the error.Thanks.

Any help will be appreciated.

CharlesEF
Just in case, your error is here:
CODE
WHERE email =".'$Email.';
You have an un-terminated string. You want PHP to convert $Email so it must be enclosed by double quotes, like this:
CODE
WHERE email ="$Email";
CharlesEF
Correction: Since the entire query should be enclosed with double quotes all you need is:
CODE
WHERE email = '$Email';
tudsy
Hi

I have another problem.

Here is a query:

$query = "CREATE VIEW ecovib2d AS
SELECT Customers.*,subscribers.*,CustomersArt.*,CustomersVideo.*,emailcomm.*
FROM Customers
FULLJOIN CustomersArt ON (CustomersArt.Itemid = Customers.Itemid )
FULLJOIN CustomersVideo ON (CustomersVideo.Itemid = Customers.Itemid )
FULLJOIN subscribers ON (subscribers.Itemid = Customers.Itemid )
FULLJOIN emailcomm ON (emailcomm.Itemid = Customers.Itemid )


WHERE email ='$Email'";


the runtime error is as follows:

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 'FULLJOIN CustomersArt ON (CustomersArt.Itemid = Customers.Itemid ) ' at line 4


Any help will be greatly appreciated.Thanks.
CharlesEF
I don't see anything wrong, as far as syntax. But, I've never heard of 'FULLJOIN'. I've heard of 'FULL OUTER JOIN' and 'FULL INNER JOIN'. Maybe it should be 'FULL JOIN'?
tudsy
QUOTE(CharlesEF @ Aug 16 2018, 02:43 PM) *

I don't see anything wrong, as far as syntax. But, I've never heard of 'FULLJOIN'. I've heard of 'FULL OUTER JOIN' and 'FULL INNER JOIN'. Maybe it should be 'FULL JOIN'?



Hi

Thanks for that.

I put commas at the end of each join and iot got rid of that error. However, I get this error now:

Error Message: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'FULL'

I have attached the rest of the code.Click to view attachment Click to view attachment
CharlesEF
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
QUOTE(CharlesEF @ Aug 22 2018, 05:13 AM) *

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
QUOTE(tudsy @ Aug 26 2018, 12:48 AM) *
$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
         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'";
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?
tudsy
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
QUOTE(tudsy @ Sep 4 2018, 11:49 PM) *

$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";
So, you're telling me that table 'Customers' has a column named 'Customersitemid' and table 'CustomersArt' has a column named 'CustomersArtitemid'?
Or, does table 'Customers' have a column named 'Itemid' and does table 'CustomersArt' have a column named 'Itemid'?

Which is correct?
tudsy
QUOTE(CharlesEF @ Sep 5 2018, 02:41 PM) *

QUOTE(tudsy @ Sep 4 2018, 11:49 PM) *

$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";
So, you're telling me that table 'Customers' has a column named 'Customersitemid' and table 'CustomersArt' has a column named '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
Create a new PHP script and place this code in it.
CODE
<?php
$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();
?>
Be sure to put your database login information in. Let me know the results of this test.
tudsy
QUOTE(CharlesEF @ Sep 6 2018, 02:54 PM) *

Create a new PHP script and place this code in it.
CODE
<?php
$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();
?>
Be sure to put your database login information in. Let me know the results of this test.



Hi

Thanks for that.

The test produced this:

NULL
CharlesEF
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'?
tudsy
QUOTE(CharlesEF @ Sep 7 2018, 12:56 PM) *

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
QUOTE(CharlesEF @ Sep 7 2018, 12:56 PM) *

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.

CharlesEF
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?
tudsy
QUOTE(CharlesEF @ Sep 7 2018, 03:10 PM) *

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
QUOTE(tudsy @ Sep 7 2018, 01:05 AM) *

I want the visitor to look at his/her data (the whole database) using his/her email address.
Then why have you been using ItemId in the ON condition? That can be fixed. One thing I don't understand, when you say visitor do you mean a customer? If yes then why was there no data in the Customers table? I mean, there was 1 row of data in CustomersArt which telles me a customer bought some art from you. Then why wasn't there a customer in the Customers table? And, how does emailcomm and subscribers tie in to the Customers table? Maybe you need a better database layout design, I mean you do seem to duplicate data between the tables.
CharlesEF
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
QUOTE(CharlesEF @ Sep 8 2018, 08:10 AM) *

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
QUOTE(tudsy @ Sep 7 2018, 08:50 PM) *
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
Great, that is what should be returned, based on the contents of the database you posted before.

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
QUOTE(CharlesEF @ Sep 8 2018, 11:38 AM) *

QUOTE(tudsy @ Sep 7 2018, 08:50 PM) *
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
Great, that is what should be returned, based on the contents of the database you posted before.

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
QUOTE(tudsy @ Sep 8 2018, 01:08 PM) *

QUOTE(CharlesEF @ Sep 8 2018, 11:38 AM) *

QUOTE(tudsy @ Sep 7 2018, 08:50 PM) *
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
Great, that is what should be returned, based on the contents of the database you posted before.

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
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')
        UNION
        (SELECT * FROM `CustomersArt` WHERE `emailadd` = '$email')
        UNION
        (SELECT * FROM `emailcomm` WHERE `emailaddress` = '$email')
        UNION
        (SELECT * FROM `subscribers` WHERE `emailsub` = '$email')";
If you also need to know where each row comes from then the query can be change to show this.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2018 Invision Power Services, Inc.