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
Related
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
I could not find an answer by searching as I am not sure what exactly it would be called what I'm searching for.
Anyways, I have multiple tables in MySQL and am trying to "fill in" some of the final product.
myTable
id assigned_to location
1 2 3
2 2 3
3 3 3
myUsers
id name
1 John
2 David
3 Sally
myLocation
id name
1 SAT
2 DEN
3 AUS
Basically the end product should pull the "myTable" data and fill into a table (which I already know how to do) the name and location of each row/column so that it states something alongm the lines of
ID Assigned To Location
1 David SAT
Instead of
ID Assigned To Location
1 2 2
This should produce the expected result:
SELECT mt.id, mu.name, ml.name
FROM mytable mt JOINT myUsers mu ON mt.assigned_to = mu.id
JOIN myLocation ml ON mt.location = ml.id
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
I would really appreciate your help on this:
Item_ID Buy_User Buy_Date Buy_Qty
00001 Adam 01/02/2013 1
00003 John 01/01/2013 2
00004 Peter 02/01/2013 1
00001 Nial 01/01/2013 1
Above is an example of my table. What I need is to sql query the Item_ID column, and display each Item_ID only once - but display each of the user's who have bought that product.
I need the query to be outputted in HTML/css.
For example my html page may look like:
Item 1: Adam bought 1. Nial bought 1.
Item 2: (would be hidden as no purchases).
Item 3: John bought 2.
Item 4: Peter bought 1.
I'm not quite sure how to achieve this, I've been googling around and found multiple mentions of sub selects, but i'm not sure how i'd get it to display each row?
Help?!
Many thanks.
Is this what you're looking for? It uses GROUP_CONCAT to group the rows and CONCAT to build the strings:
SELECT Item_Id, GROUP_CONCAT(CONCAT(Buy_User,' bought ',Buy_Qty) SEPARATOR ', ') ITEMS
FROM Items
GROUP BY Item_Id
Here is the SQL Fiddle.
And here are the results:
ITEM_ID ITEMS
00001 Adam bought 1, Nial bought 1
00003 John bought 2
00004 Peter bought 1
I have a database that has id numbers, names, class period, and teacher name. Each row includes a student's id, name, period, and teacher which means that there are multiple rows for each student. That's not my problem. My problem is that some classes have two teachers listed...which means there are two separate rows for 1 class period, one for each teacher. Unfortunately I have no control over the formatting of the data since it's exported from a system that doesn't have a lot of formatting options.
Here is an example, notice how rows 23 and 24 are for the same class period and student, but the only thing different is the teacher names.
pk id last first period teacher
14 12345 Smith John 3 HARRIS
15 12345 Smith John 8 LEAL
17 12345 Smith John 1 HOUSTON
23 56789 Doe Jane 8 MERCER
24 56789 Doe Jane 8 RUIZ
25 56789 Doe Jane 3 BECK
26 56789 Doe Jane 1 STEED
I would like to combine the two rows with the same period number and student name into one row. All the information would remain the same except the two different teacher's names would be combined into something like "Mercer & Ruiz." Ideally the final result would look something like,
24 56789 Doe Jane 8 MERCER & RUIZ
Is this possible using PHP and/or MySQL? I'm not looking for anyone to write the entire code or anything. I just can't seem to think of a way to accomplish it. I'd be happy with any direction/indication of a way to go about this.
As always, thanks for your time and help.
SELECT MAX(pk),
ID,
`last`,
`first`,
GROUP_CONCAT(teacher SEPARATOR ' & ') teachers
FROM tableName
// WHERE clause here...
GROUP BY `last`, `first`, `Period`
// ORDER BY clause here....
SQLFiddle Demo
SOOURCE
GROUP_CONCAT()
SELECT pk,id,last,first,period GROUP_CONCAT(teacher)
FROM table
GROUP BY id, first,last,period
Should do what you need.