Merging and converting rows to columns - mysql

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/

Related

Mysql stored procedure Pass Guid list to parameter and use for in clause [duplicate]

I have a table like:
id name children
1 Roberto Michael,Dia
2 Maria John,Alex
3 Mary Alexandre,Diana
My problem is;
I want to find who has a child named Alex.
I can't use "where children = 'Alex'" in SQL because I have more than one names in same cells.
So I use "where children LIKE '%Alex%'" - that looks smart but
in the same time i get all start like Alex :( Alexandre
or i want to get dia but result is dia and diana :(
how can I get single Alex in that data type?
I hope I can explain my problem with my terrible english :D
The best solution would be to normalize your schema. You should have a separate table with one row for each child, instead of a comma-delimited list. Then you can join with this table to find parent with a specific child. See #themite's answer for an example of this.
But if you can't do that for some reason, you can use FIND_IN_SET:
WHERE FIND_IN_SET('Alex', children)
You should split the data into two tables.
the first would look like this
ID Name
1 Roberto
2 Maria
3 Mary
And the second like this
ParentId child
1 Michael
1 Dia
2 John
2 Alex
and so on.
then you could do the query you want without having to worry about like and your data is much more useable
That's why you'd want to have two tables here.
parents:
id name
1 Roberto
2 Maria
3 Mary
children:
id parentid name
1 1 Michael
2 1 Dia
3 2 John
4 2 Alex
5 3 Alexandre
6 3 Diana
And now you can query this much more effectively with a join or an exists:
SELECT *
FROM Parents
WHERE EXISTS(
SELECT *
FROM Children
WHERE parentid=Parents.id
AND Children.name='Alex'
)
I would rather make different tables for children and parents something like this.
Table for parents
parent_id name
1 Roberto
2 Maria
3 Mary
Table for children
children_id parent_id name
1 1 Michael
2 1 Dia
3 2 John
.... and so on

List contents of row in a table - but only once

I have a table where one field contains a free to choose text. Some of these texts are identical, some are not. So as an example, this may look like this:
Joe
Jim
Jack
Jack
Jim
Jack
Jane
Now I want to list all the contents of these fields, but compared on a content level so that every data is shown only once. Means the result from my data have to look like this:
Joe
Jim
Jack
Jane
The double-entries Jim and Jack are shown only once although they are contained more often.
My question: is there a SQL-statement which covers this or do I have to filter these data in the result?
Thanks!
Select distinct fieldname from your_table_name
SELECT DISTINCT nameField
FROM YourTable

How to check if the result of DAYOFWEEK(CURDATE()) is exist in a column? [duplicate]

I have a table like:
id name children
1 Roberto Michael,Dia
2 Maria John,Alex
3 Mary Alexandre,Diana
My problem is;
I want to find who has a child named Alex.
I can't use "where children = 'Alex'" in SQL because I have more than one names in same cells.
So I use "where children LIKE '%Alex%'" - that looks smart but
in the same time i get all start like Alex :( Alexandre
or i want to get dia but result is dia and diana :(
how can I get single Alex in that data type?
I hope I can explain my problem with my terrible english :D
The best solution would be to normalize your schema. You should have a separate table with one row for each child, instead of a comma-delimited list. Then you can join with this table to find parent with a specific child. See #themite's answer for an example of this.
But if you can't do that for some reason, you can use FIND_IN_SET:
WHERE FIND_IN_SET('Alex', children)
You should split the data into two tables.
the first would look like this
ID Name
1 Roberto
2 Maria
3 Mary
And the second like this
ParentId child
1 Michael
1 Dia
2 John
2 Alex
and so on.
then you could do the query you want without having to worry about like and your data is much more useable
That's why you'd want to have two tables here.
parents:
id name
1 Roberto
2 Maria
3 Mary
children:
id parentid name
1 1 Michael
2 1 Dia
3 2 John
4 2 Alex
5 3 Alexandre
6 3 Diana
And now you can query this much more effectively with a join or an exists:
SELECT *
FROM Parents
WHERE EXISTS(
SELECT *
FROM Children
WHERE parentid=Parents.id
AND Children.name='Alex'
)
I would rather make different tables for children and parents something like this.
Table for parents
parent_id name
1 Roberto
2 Maria
3 Mary
Table for children
children_id parent_id name
1 1 Michael
2 1 Dia
3 2 John
.... and so on

Limit MySQL End User to Viewing His Own Entries Only

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.

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