SQL Select the column if the value is like with multiple columns - php

I need to create a autocomplete search with ajax. The suggestions should only contain the 10 most entered results. The search query has to check multiple columns if the value is like my variable.
But my problem is to create the query and the php logic for that.
Is there any plugin or something simular for that?
How can I select a column if the value in it is like my variable?
I need to create a count query, which counts (in all columns) "how often is here the full word (splitted by spaces)" <- which is like the found one (to get the relevance)
At the end I need to sort the found entries by their relevance to provide the 10 most relevant entries.
(The real query checks for more columns than just 2, but for dummy reasons are 2 okay)
The query which selects the rows where the value is like...
select * from
(
(select department from entries where department like '%myVariable%')
OR
(select grade from entries where grade like '%myVariable%')
)
I think you know what I mean. Does anyone have any hints, suggestions, examples or useful links for me?
Thanks in advance!
Best regards,
FriaN

Why not use union all here?
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%'
Then this should order the results for you:
select department, count(*) cnt from (
select department from entries where department like '%myVariable%'
union all
select grade from entries where grade like '%myVariable%')a
group by department
order by count(*) desc

Related

How to Show Top Value filed as top in Mysql

I want to show the college name and num of applications comes to each college and show most application received college as first position in MySQL query?
This is sample table, clg is college name and application_name column must be count() and highest value field must be show on top
My results must be like this....
try this,
SELECT `clg`,count(`clg`) AS clg
FROM demo
GROUP BY `clg`
ORDER BY clg DESC
I am answering this because the columns in a select should be unique, particularly when they are referenced in the order by clause. The query should look more like:
SELECT s.clg, count(*) as cnt
FROM sample s
GROUP BY s.clg
ORDER BY cnt DESC ;

Multiple table select grouped query

We need to grab the last and newest 20 entries from different tables. However, the GROUP BY statement skips records because we are working with LEFT JOIN on tables.
All these records are linked to unique persons in another table. We store these person's id's in an array for more queries later.
We have a few tables (in which all those person id's are stored) and we want to get them sorted and grouped.
The tables are like this:
SELECT lastRecord+personID FROM t1
SELECT lastRecord+personID FROM t2
SELECT lastRecord+personID FROM t3
SELECT lastRecord+personID FROM t4
WHERE t5.Essential_Column_Name = '1'
GROUP BY personID
ORDER BY 'all the latest entries'
LIMIT 20
With that, the relevance of all the latest entries should be equal.
We do have a timestamp column as well. Perhaps that might work better.
Any input is highly appreciated!
For people looking for an answer on this; this is the right post, answer and update to this Q:
UNION mysql gives weird numbered results
With thanks to all for the ideas and providing the paths to the right solution.

How to get only 1 row of each similar content?

I Have a private message system on my site and i'm trying to pull all subject's on the messages.
I need to show any subject once.
so if I have this subjects:
hey
hello
hey
good morning
good morning
I need to print this:
hey
hello
good morning
I can just cross it out with if, but I guess there is a better way with sql.
Thank you.
Option 1 : DISTINCT
SELECT DISTINCT subject
FROM my_table
Option 2 : GROUP BY
SELECT subjects
FROM my_table
GROUP BY subjects
Difference between GROUP BY and DISTINCT
Distinct is used to filter unique records out of the records that satisfy the query criteria.
Group by clause is used to group the data upon which the aggregate functions are fired and the output is returned based on the columns in the group by clause. It has its own limitations such as all the columns that are in the select query apart from the aggregate functions have to be the part of the Group by clause.
See this and this for the reference .
Try something like:
SELECT DISTINCT subject FROM emails;
If you use SQL.
select distinct subjects
from YOUR_TABLE.
or
Select subjects
from YOU_TABLE
group by subjects

Count filter records in PHP/MYSQL [duplicate]

This question already has answers here:
Count search criteria record based on search done by user (MYSQL PHP)
(3 answers)
Closed 9 years ago.
I have a PHP/MYSQL based application in which there is a search area, when you search by dates then I show that a property/hotel is available between those dates. Also I have some other filters like area, facilities that Hotel has etc.
Now as of now everything working ok, but cutsomer now wants to show the number of records in bracket for each filter.
I tried it by adding multiple queries for each filter based on dynamic search user did, but that making my page performance slow. Because if I have 5 filters then I will run 5 queries.
I have seen such thing in magento, it counts the number of result that filter have as shown in picture below:
What will be the best method of doing this, I just need some logic and procedure which can be followed to resolve this.
Posting whole table structure is difficult, but I am positing shorter for of it, so you guys may have some idea and suggest some solution:
Tables are:
Properties - id, name
Factsheet_label - label_id, Name
Factsheet - id, label_id, prop_id, value (Yes, No)
I am showing all the filters from factsheet_label table and then I need to count the result of properties I have.
A statement like this
SELECT categoryId,CategoryName,COUNT(*) FROM Products GROUP BY CategoryId,CategoryName
will group the results by category and return a count of each, like so
CategoryId CategoryName Count
1 Living Room 4
2 Bedroom 2
EDIT
This should do it, if I've understood your tables correctly.
SELECT F.id,F.label_id,F.prop_id,F.value,L.Name,
(SELECT COUNT(*) FROM Properties WHERE id = F.prop_id)
FROM FactSheet F
LEFT JOIN FactSheet_label L on L.label_id = F.Label_id
All data is pulled from Factsheet. the relevant label is pulled from factsheet_label. The count is then retreived for each row via a subquery
Here's another way to do it, which may or may not be a little more efficient.
SELECT F.id,F.label_id,F.prop_id,F.value,L.Name,P.count
FROM FactSheet F
LEFT JOIN FactSheet_label L on L.label_id = F.Label_id
LEFT JOIN (SELECT id,COUNT(*) count FROM Properties GROUP BY id) P ON P.id = F.prop_id
Every "SELECT *" can be a "SELECT *, COUNT(*) ...." then you can see the nr of the results.
To help you more I need to see the code.
Hope this is helpful
SELECT (
SELECT COUNT(*)
FROM tab1
) AS count1,
(
SELECT COUNT(*)
FROM tab2
) AS count2
FROM dual

Include NULL as 0 in COUNT SQL Query

I know for a fact this has been asked a few times before, but none of the answered questions relating to this seem to work or are far too confusing for me..
I should probably explain.
I'm trying to create an AJAX script to run to order some results by the number of 'Likes' it has.
My current code is this:
SELECT COUNT(*) AS total, likes.palette_id, palette.*
FROM likes LEFT JOIN palette ON likes.palette_id = palette.palette_id
GROUP BY likes.palette_id
ORDER BY total DESC
Which works fine, however it doesn't list the results with 0 likes for obvious reasons, they don't exist in the table.
I've attached images of the current tables:
Likes table:
http://imgur.com/EGeR3On
Palette table:
http://imgur.com/fKZmSve
There are no results in the likes table until the user clicks 'Like'. It is then that the database gets updated and the palette_id and user_id are inserted.
I'm trying to count how many times *palette_id* occurs in the likes table but also display 0 for all palettes that don't appear in the likes table.
Is this possible? If so, can someone help me out at all?
Thank you
It might not be the exact MySQL syntax (I'm used to SQL Server), but should be pretty straight forward to translate if needed.
SELECT p.*, IFNULL(l.total, 0) AS total
FROM palette p
LEFT JOIN (
SELECT palette_id, COUNT(*) AS total
FROM likes
GROUP BY palette_id
) l
ON l.palette_id = p.palette_id
ORDER BY total
Try this:
SELECT COUNT(likes.palette_id) AS total, palette.palette_id, palette.*
FROM palette LEFT JOIN likes ON likes.palette_id = palette.palette_id
GROUP BY palette.palette_id
ORDER BY total DESC
EDIT:
In regards to the discussion about listing columns that are not in the GROUP BY, there's a good explanation in this MySql documentation page.
MySQL extends the use of GROUP BY so that the select list can refer
to nonaggregated columns not named in the GROUP BY clause. This means
that the preceding query is legal in MySQL. You can use this feature
to get better performance by avoiding unnecessary column sorting and
grouping. However, this is useful primarily when all values in each
nonaggregated column not named in the GROUP BY are the same for each
group. The server is free to choose any value from each group, so
unless they are the same, the values chosen are indeterminate.
In this example, the palette information not added to the GROUP BY will be the same for each group because we are grouping by palette_id so there won't be any issue using palette.*
Your join is written backwards. It should be palette LEFT JOIN likes, because you want all rows in palette and rows in likes, if they exist. The "all rows in palette" will get you a palette_id for the entries there without any matching "likes."

Categories