order by specific word count in mysql - php

I need to order the results based on the repeatness of the word using mysql.
Here is my sample table
id Name keywords Description
1 John John, USA John is good boy. John, John
2 Alex Alex, John Alex is a friend of john.
3 Rocky John Rocky
4 John John,John John, John, John, John, John
Will take an "John" as example. In first row "John" is repeated 5 times, 2 times repeated in second row, 1 time repeated in 3rd row and 8 times repeated in 4th row. I need to show the results based on the count descending.
Select * From table Where name like '%John%' OR keywords like '%John%' OR Description like '%John%'
So it will show in below order
id Name keywords Description
4 John John,John John, John, John, John, John
1 John John, USA John is good boy. John, John
2 Alex Alex, John Alex is a friend of john.
3 Rocky John Rocky

This will do the trick:
SELECT id,Name,keywords,Description,
ROUND (
(
LENGTH(CONCAT(Name,keywords,Description))
-LENGTH(REPLACE( CONCAT(Name,keywords,Description), "John", "") )
) / LENGTH("John")
) AS count
FROM tbl ORDER BY `count` desc
see here: Demo
Update
If you want to look for multiple (different) words per record you
you should use a user-defined function (UDF) like
CREATE function wcnt(wrd varchar(32), str varchar(1000)) returns int
RETURN ROUND ((LENGTH(CONCAT(str))-LENGTH(REPLACE( CONCAT(str),wrd,"")))/LENGTH(wrd));
see here: function-demo
or here for a combination of the first query with the UDF

SELECT * FROM T
ORDER BY FIND_IN_SET('John',Description) DESC
FIDDLE

Related

checking for specific text in a mysql field inside a query [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

PHP get user if they fit group [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

Query with multiple values in a column

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

PHP MySQL combine cells from different row with same value

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.

Sql Distinct Count of Duplicates

I am doing a query on a mysql database. I have a main table where users are stored and another table where friends of that user are stored. For each user I want to see how many
friends they have. This is what I'm getting.
ID FirstName LastName FriendID
1 Andrew Smith 1
1 Andrew Smith 5
1 Andrew Smith 9
2 John Doe 3
2 John Doe 5
This is what I want to get.
ID FirstName LastName Friends
1 Andrew Smith 3
2 John Doe 2
If this is not enough detail to go on let me know and I will also show the tables and query I used.
SELECT ID, FirstName, LastName, COUNT(FriendID) AS Friends
FROM Users GROUP BY ID, FirstName, LastName
One hopes you're not really storing FirstName and LastName in every UserFriendLink record. If you are, it's time to normalize your database with a Users table (with ID, First, and Last) and a UserFriendsLink table (with UserID and FriendID).

Categories