I have two tables:
mysql> explain Customers
-> ;
+------------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-------------+------+-----+---------+-------+
| customerNumber | int(11) | NO | PRI | NULL | |
| customerName | varchar(50) | NO | | NULL | |
| contactLastName | varchar(50) | NO | | NULL | |
| contactFirstName | varchar(50) | NO | | NULL | |
| phone | varchar(50) | NO | | NULL | |
| addressLine1 | varchar(50) | NO | | NULL | |
| addressLine2 | varchar(50) | YES | | NULL | |
| city | varchar(50) | NO | | NULL | |
| state | varchar(50) | YES | | NULL | |
| postalCode | varchar(15) | YES | | NULL | |
| country | varchar(50) | NO | | NULL | |
| salesRepEmployeeNumber | int(11) | YES | | NULL | |
| creditLimit | double | YES | | NULL | |
+------------------------+-------------+------+-----+---------+-------+
13 rows in set (0.00 sec)
And my other table is:
mysql> explain Employees;
+----------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+---------+-------+
| employeeNumber | int(11) | NO | PRI | NULL | |
| lastName | varchar(50) | NO | | NULL | |
| firstName | varchar(50) | NO | | NULL | |
| extension | varchar(10) | NO | | NULL | |
| email | varchar(100) | NO | | NULL | |
| officeCode | varchar(10) | NO | | NULL | |
| reportsTo | int(11) | YES | | NULL | |
| jobTitle | varchar(50) | NO | | NULL | |
| salesGoal | decimal(10,0) | YES | | NULL | |
+----------------+---------------+------+-----+---------+-------+
9 rows in set (0.00 sec)
I am attempting to write a view to limit the sales reps from being to see only the customers they have entered in the system. The problem I am having is the sales rep user name is the same as their email address. I'm having problems with my where clause to allow them to view their customers while logged in.
So far my view will work with:
SELECT
cu.customerNumber AS ID,
concat(cu.contactFirstName,_utf8' ',cu.contactLastName) AS Name,
concat(cu.addressLine1,_utf8' ',cu.addressLine2) AS Address,
cu.postalCode AS 'Zip Code',
cu.phone AS Phone,
cu.city AS City,
cu.country AS Country
FROM
Customers cu
JOIN Employees s on (cu.salesRepEmployeeNumber = s.employeeNumber)
WHERE employeeNumber = 123456;
I've been trying to use something like
SUBSTRING_INDEX(user(), '@', 1)
to get the username, but I am not sure how to match that to my email column in the Employees table. Am I correct in trying to match it with another substring? Something like:
SELECT
cu.customerNumber AS ID,
concat(cu.contactFirstName,_utf8' ',cu.contactLastName) AS Name,
concat(cu.addressLine1,_utf8' ',cu.addressLine2) AS Address,
cu.postalCode AS 'Zip Code',
cu.phone AS Phone,
cu.city AS City,
cu.country AS Country
FROM
Customers cu
JOIN Employees s on (cu.salesRepEmployeeNumber = s.employeeNumber)
WHERE SUBSTRING_INDEX(user(), '@', 1) =
(SELECT SUBSTRING_INDEX(email, '@', 1) FROM Employees);
I'm probably making this way harder than it needs to be, and I'm sure I am missing something simple!
Thanks in advance!