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
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 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
id_no doc_id item_no product customer
123 2 1 A Daisy
123 2 9 A Ben
123 4 3 A Daisy
123 4 4 A Ben
123 6 11 B Daisy
123 6 13 B Ben
when I put it in my report it results to
Daisy Daisy
Ben
And it is also the result in mysql
select distinct customer from receipt where id_no like '123'
result:
Daisy
Daisy
Ben
Another query that I tried:
select distinct id_no, customer, product from receipt where id_no like '123'
result:
123 Daisy A
123 Daisy B
123 Daisy A
123 Ben A
123 Ben B
desired result:
Daisy
Ben
Please help me please.
Thank you guys for the help I found out why the other one keeps on showing. It is because the other Daisy is spelled as Daissy that's why.
Most likely your Customer name contains additional characters between the two records. Depending on how the datatype is implemented, spaces could matter and have contributed to the difference.
Try concatenating a character before and after customer.
I am unfamiliar with the concepts in Crystal Reports, but from what I understand, you would have to create a formula like so:
"XXX" & {Receipt.Customer} & "XXX"
If you run it again, you might recognize there is additional space like so:
XXXDaisyXXX
XXXDaisy XXX
^____ Additional Space
There is no chance of error while you using distinct ..it should return distinct value ...any way you can try another way
SELECT customer FROM receipt WHERE id_no like '123' GROUP BY customer
I don't see why you are fetching three records. I tried implementing your database and ran your query. It returned the result as expected.
See the above pic. There may be some issue with the data type you used. You may try grouping via customer, but I don't think it should affect your result anyway.
Also Check if the data types match.
The selection you made from customer id and id_no is unique and with distinct it should return only two rows
plase try this code
i get solution
select distinct `customer` from receipt where `id_no`='123'
this is right
i tryied this is my past project
best of luck
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/
-------------------------------
name | address
-------------------------------
raj kumar | park street
yogin patel | ghari chowk
raju singh | sultan ganj
I searched for park road in table and I could not find it(that's ok).But what query will I use such that I search for parkroad (no space between the words) and at least I receive a details of row containing either park or road or both?
Please give some general Query so that it can be applied for all words whichever I want to search?
Thanks in advance
Maybe something along the lines of:
SELECT name, address FROM table_name
WHERE address LIKE '%park%' or address LIKE '%road%';
You should try this out more to identify what you're exactly looking for.