Mysql -> MATCH AGAINST FULL TEXT SEARCH - mysql

I have a table with millions of records, for example:
id Name Age
1 john 23
2 peter 27
3 moses 23
....
....
Like this using MySQL MATCH AGAINST query. I want to pick the third row which has
name moses & age 23.
I can use an ordinary query like:
select from table where name='moses' && age ='23'
But this takes a lot of time.
So, my friend told me to use a Match Against Query.
Can anyone tell me whether we can do this in a Match Against Query and its advantages?
How can we write the above query using a MATCH() ... AGAINST syntax?

SELECT *
FROM table AS t
WHERE MATCH(t.Name) AGAINST('moses')
AND t.Age=23

Related

Do 'where` conditions count affect select speed in mysql?

There is a mysql Person table like below. id is primary, indexed. But other columns are not indexed..
id name surname age city branch
1 John Black 34 London driver
2 Lara Croft 28 New York teacher
3 Ahmad Hasan 41 Doha doctor
...
1000.000......
My Question is when I make execute select query with where clause with multiple conditions, does it decrease select speed.
For example which one is faster?
Select * From Person Where age > 30
or
Select * from Person Where age > 20 AND city = 'London' AND name = 'John' AND branch = 'doctor' AND ...
Could you tell me which one will be faster?
Without indexes, any WHERE clause causes a table scan. That is, to satisfy the query the server must examine every row in the table. So the search operations you have shown take on the order of the same time as one another.
It also takes time to send a large result set from the MySQL server to the client. Fewer rows in the result set make that part of satisfying your query faster.
Pro tip: avoid SELECT * when dealing tables over about 100 rows long. Instead give the names of the columns you actually need.

Find a string in one column if exist than give that value othere wise find in other column in a single mysql query

I have 3 column in database with 20000 records.
Let take a example
id | country_string | country_code<br>
275| Bangalore,BLR-India | BLR<br>
375| Basongo,BAN | BAN
I have given one record. I'm searching BLR. First it should match from country_code column if there then it will return the record otherwise it should search in country_string.It should be in single query.
if I'm using LIKE query And I search BAN It will give both record but It should come record number 375 and searching bang it should give record number 275
You can do this with LIKE.
Query
SELECT * FROM tableName
WHERE country_code LIKE '%BAN%'
OR country_string LIKE '%,BAN%';
select * from table where
country_code like '%$search_name%'
OR country_string like '%$search_name%'

Find lowest value and coresponding code on asingle query mysql

I have a user table as follows
id name age
1 John 21
2. Mathan 23
3. Raj 21
4. Manoj 50
5 Krishnan 91
I want to find minimum age and its corresponding name. How can I do it with rails?
Can I do it in a single query?
Note: More than one names can have single age.
Is there a specific reason why you want to do it in a single query ?
If you can write 2 queries, I think you can just write :
User.where age: User.minimum(:age)
select age, group_concat(name) from table group by age order by age asc limit 1
You will need to process the results later on in ruby, but this gives all you need in one single query. Also i am assuming mysql, so might differ on other rdbms.
It gives exact output in mysql that you want try this
SELECT concat("[",name," ",age,"]") AS name
FROM TABLE
WHERE age =
(SELECT min(age)
FROM TABLE);

Counting the number of pattern occurrences in a certain column with MySQL

I want to search the column Name of a certain table to see how many rows have a Name value that matches a certain pattern. For example, if the pattern I am looking for is %Peter% and in this table there are 5 rows with the Name values:
Peter
Peter Smith
George Peter
Peter Peter
Carl
I want to obtain the value 4. I tried to use COUNT, but don't know how to combine it for example with LIKE. How do I go about to do this?
Not sure why didn't LIKE work for you, but this SHOULD do the trick:
SELECT COUNT(*) FROM table
WHERE name LIKE '%Peter%'

mysql combining records from one table

I have a single table that uses test# as the primary key. Here is what that table looks like:
Test# Name VerbalScore readingScore Notes
1 Bobby 92 Good job
2 Bobby 40 You Suck Bobby
The problem is I want to view and be able to see when there are multiple verbal scores for the same Name (so be able to see if the person took the same test more than once).
I want to have some kind of select statement to get this result from the above table:
1 Bobby 92 40 Good job, You Suck Bobby
Is that possible?
I am not totally sure I understand what you mean by "see when there are multiple verbal scores" but with mysql 5+, try
SELECT
Name,
GROUP_CONCAT(VerbalScore),
GROUP_CONCAT(readingScore),
GROUP_CONCAT(Notes)
FROM
myTable
GROUP BY
Name;
GROUP_CONCAT is a mysql specific grouping function.