Limit MySQL End User to Viewing His Own Entries Only - mysql

My Table is like this
ID FileNumber SellerRep BuyerRep Address
123 142366 John Doe Steve Doe 123 Cute Ave
122 142365 Steve Doe John Doe 412 Best Blvd
121 142364 John Doe John Doe 234 Park Ave
120 132363 Steve Doe Steve Doe 233 Cool St
119 132362 Steve Doe Frank Good 432 Bad Street
users: johnd (lists his name as John Doe)
steved (lists his name as Steve Doe)
How can I limit end user to view/see only his own entries. His entries always include his name either in SellerRep or BuyerRep columns, and sometimes in both though they may possibly be misspelled (e.g. Steven Do, etc). However, either SellerRep or BuyerRep columns may include name of the non user as in the entry with ID # 119 in the table example.
So, basically, if Steve Doe logs in, I want him to see only entries with ID # 119, 120, 122, 123, and if John Doe logs in, I would like to limit his views only to records with ID # 123, 122, 121. One issues though is that One of them may have specify the full name of another user in either SellerRep or BuyerRep column, and I wouldn't want in this case the other user to see this entry.
Is there a way to accomplish this with views or triggers and not in PHP page, even if I have to add another column like #user with auto_complete if that possible.
The least what I could think of to start with is:
CREATE VIEW saleslogview
AS
SELECT *
FROM saleslog
WHERE SellerRep = 'Steve Doe';
CREATE VIEW saleslogview
AS
SELECT *
FROM saleslog
WHERE BuyerRep = 'John Doe';
CREATE VIEW saleslogview
AS
SELECT *
FROM saleslog
WHERE BuyerRep = 'Steve Doe';
CREATE VIEW saleslogview
AS
SELECT *
FROM saleslog
WHERE SellerRep = 'John Doe';
However, when I tried to log in as user steved or johnd with like SQLBuddy and tried to search records it did not work as intended - all records were "visible".
Perhaps there is a way to accomplish this using SESSION_USER(), but I don't know anything about it.

Related

MySQL values to be in one column just separated by a comma

So I have a query that is pulling alot of data together and I would like it to only have one row vs x amount, and it just be one but the column Product ID, which is the last one, to just be all on one line, example data:
ID FN LN MEM REP EMAIL PID
001 Test User 1001 1001 testemail#gmail.com 001
001 Test User 1001 1001 testemail#gmail.com 002
001 Test User 1001 1001 testemail#gmail.com 003
001 Test User 1001 1001 testemail#gmail.com 004
001 Test User 1001 1001 testemail#gmail.com 005
001 Test User 1001 1001 testemail#gmail.com 006
001 Test User 1001 1001 testemail#gmail.com 007
But would like the output to be:
001 Test User 1001 1001 testemail#gmail.com 001,002,003,004,005,006,007
My SQL knowledge is not super strong so im kinda lost, any help would be awesome. I tried GROUP BY but some of the data as different values in the fourth column so it wont always work.
You're looking for GROUP_CONCAT along with GROUP BY.
SELECT ID, LN, EMAIL, GROUP_CONCAT(PID) as products
FROM tableName
GROUP BY ID
Or something like that. GROUP_CONCAT(productID) will combine them into one row and GROUP BY tells it how to combine the rows together.
If you remove the GROUP BY, you will get one row with all the results found. If you add it, it tells it how to combine the rows, which field to match.
GROUP_CONCAT is probably what you're looking for. So you can group and concatenate the Product ID and group it by a common attribute of your data records.
Try GROUP_CONCAT()
It's the same thing as Stuff For XML in TSQL

Merging and converting rows to columns

I have a list of data that happens to have been set up like this:
entryID fieldID Value
100 1 John
100 2 Smith
100 3 USA
101 1 Jane
101 2 Doe
101 3 USA
The way it works is, when a "ticket" is created, it assigns an entryID for all the data in that ticket, but each field also has it's own fieldID. So first name is always a fieldID of 1, last name is 2, etc.
What I need to be able to do is create a view that will look like this:
First Name Last Name Country
John Smith USA
Jane Doe USA
I need to be able to do this in MySQL, and not SQL or excel, as some other similar problems have addressed.
I just found the answer to my own question, so I will post it here for future users.
This website explains how to do exactly what I was asking for: http://stratosprovatopoulos.com/web-development/mysql/pivot-a-table-in-mysql/

SQL Database with undefined columns

I'm working on a project where I'm dumping data into a mysql database about different people, however, each person can have many entries so I'm not sure how to have a lot of columns.
e.g
Name id
jack 234 01241990 13241990 03451993 10945
james 222 01131998 14242001 03414235 10945435 3456363 3465758
jill 1234 01131998 14242001 03414235 10945435 3456363 3465758 4253156316 6427247 583583
As you can see there can be many entries for each person, not in 100's, but I think the max can be around 20-30ish? So how do I build a database that I can insert values into without knowing how many entries will be per person, beforehand.
I am using perl script to insert values. Any ideas will be helpful
EDIT: People are suggesting to create two tables, however, when I joint he tables, I want one row for each person.
e.g After joining my view should look like
james 222 01131998 14242001 03414235 10945435 3456363 3465758
My suggestion would be to split the data into two tables (People and Data):
People:
NAME ID
Jack 234
James 222
Jill 1234
Data:
ID PeopleID Data
1 234 01241990
2 234 13241990
.
.
99 1234 6427247
100 1234 583583
You can then use joins to get the data for each person
SELECT p.Name,
p.ID,
d.Data
FROM People p
JOIN Data d
ON d.PeopleID = p.ID
ORDER BY p.Name --(assuming you want names in alphabetical order)
You should get something like the following
Name ID Data
Jack 234 01241990
Jack 234 13241990
.
.
Jill 1234 6427247
Jill 1234 583583

IF EXISTS in WHERE clause?

I want to insert a condition in one of my queries, but don't really know how to do it.
Basically, I'm sorting a bunch of files by their folder _id, i.e. the folder they belong to (using order by folder_id). What I'd like now, for each of these groups of files, is the query to pick the first file that satisfies a particular condition. If that condition is not satisfied by any of the files in a folder, than the query should pick the first document available in the folder.
This is my query (simplified):
select folder_id,
file_id,
first_name
from f_file
where first_name = 'John'
order by folder_id
So, if my results now are:
FOLDER ID FILE ID AUTHOR
A 32 John
A 41 John
A 56 Thomas
A 78 Peter
B 02 Peter
B 03 Peter
B 45 Peter
C 34 John
C 56 Thomas
C 77 Peter
C 86 John
D 12 Peter
D 34 Thomas
D 89 Thomas
for each folder, I'd like the query to show only the first document (lowest file_id) that was created by John. If there are no documents created by John at all in that folder, than the first one from any other author will do.
FOLDER ID FILE ID AUTHOR
A 32 John
B 02 Peter
C 34 John
D 12 Peter
So, the ID has to be the lowest, but the Author has to be John or another one.
My idea would be to insert an IF EXISTS clause in the WHERE clause, but how to do it?
Thanks for your responses.
Val

SQL - Count of a column until specific result

I have a table containing info on call attempts on contacts. The relevant fields are below:
Contact Date Result
John Smith 1/8/2013 VM
John Smith 1/9/2013 VM
John Smith 1/10/2013 Busy Signal
John Smith 1/11/2013 Hang Up <--- Connect
Jane Smith 1/8/2013 VM
Jane Smith 1/9/2013 Scheduled Call Back <--- Connect
Jane Smith 1/10/2013 VM
John Doe 1/8/2013 Not Interested <--- Connect
Jane Doe 1/8/2013 Busy Signal
Jane Doe 1/9/2013 Busy Signal
Jane Doe 1/10/2013 Busy Signal
I'm trying to get a count of the number of attempts until connect by contact (and not any attempts after the first connect), or the number of attempts so far if no connect has been made, and I'm having trouble. The results that would signify a connect in this case would be Hang Up, Scheduled Call Back and Not Interested.
What I am trying to get would look like:
Contact Attempts
John Smith 4
Jane Smith 2
John Doe 1
Jane Doe 3
Is there a way to do this? Any help would be greatly appreciated.
You need to look into using GROUP BY:
SELECT Contact, COUNT(*) Attempts
FROM Contacts
WHERE Result NOT IN ('Hang Up','Scheduled Call Back','Not Interested')
GROUP BY Contact
--EDIT
Didn't realize OP's request. And couldn't finish this before he accepted another answer...
Good luck.
For MySQL:
SELECT
t.Contact, COUNT(*) AS Attempts
FROM
tableX AS t
JOIN
( SELECT DISTINCT contact
FROM tableX
) AS d
LEFT JOIN
( SELECT contact,
MIN(date) AS date
FROM tableX
WHERE result IN ('Hang Up', 'Scheduled Call Back', 'Not Interested')
GROUP BY contact
) AS g
ON d.contact = g.contact
ON t.contact = d.contact
AND t.date <= COALESCE(g.date, '9999-12-31')
GROUP BY
t.contact ;