Order by most searched words in MYSQL - mysql

I've designed a simple website which enables users to search on a MYSQL database. The search method is also simple: the user types a word in a textfield and it searches in the database.
Now i want to include in this website a table with the most searched words and i didn't find anything until now but this sentence:
select distinct column
, count(1) as total
from dept
group by column
order by total desc
limit 5
but this doesnt retrieve what i want. Do you have any idea of how I get this result?
Thank you in advance!

A simple example for a small site:
After each search, add a row to a table searched. Bonus points for adding a timestamp.
insert into searched (keyword, timestamp) values ('foo', 1234567890);
From there:
select keyword, count(*) as total from searched
group by keyword order by total desc limit 5;
Of course, for simple things like this, I'd use redis.

Related

MySQL Statement - Select where like Concat

I have a field called "number" in my table modules. I want to show only specific entries, if the correct number is in my "number" field. Right now I am doing this with the following sql statement:
SELECT *
from modules
where number like CONCAT(
(SELECT id
from account_metadata
where hash='00cRTM')
)
ORDER BY date DESC
ID = 2385
This query is working fine as long as I have only one number insider the number field. As our system grows we need to save more than just one number insider our number field, therefore the entry could be something like this:
4235, 2385, 2058
More than one number, separated by commas.
Now my above sql query is not working anymore. Can someone help me and let me know how I should change my query to fetch all entries?
I would really appreciate any help.
Thanks in advance,
Chris
I found a solution by myself. It is working and all I need for testing purposes. Thanks to everyone who tried to help me.
SELECT *
from modules
where number like CONCAT('%',
(SELECT id
from account_metadata
where hash='00cRTM') ,'%'
)
ORDER BY date DESC

Selecting ordered and unique values from one to many mysql relationship

In phpbb3 I want to select latest 10 active topics. i.e topics that lastly has posts. In the PHPbb3 schema, the table topics has many posts. In order to achieve this, I tried the following SQL
SELECT DISTINCT (`t_topics`.`topic_id`), `t_topics`.topic_title FROM
`t_topics` , `t_posts` WHERE `t_posts`.topic_id = `t_topics`.topic_id
ORDER BY `t_posts`.post_id DESC LIMIT 10;
However, there is a topic that I'm sure that it has the latest post and it comes at the end of records.
I tried to remove DISTINCT However, I got have the correct order, but there are repeated topics. I want to get the correct order with no repeated topics but I don't know how?
You can use group by clause instead of Distinct on t_topics.topic_id.
Query is : SELECT t_topics.topic_id, t_topics.topic_title FROM t_topics , t_posts WHERE t_posts.topic_id = t_topics.topic_id GROUP BY t_topics.topic_id ORDER BY t_posts.post_id DESC LIMIT 10;

MySQL: Select last updates from two really different tables

I tried to search everywhere, but not found any answer.
I have two different tables in database with only two same columns (name, dateChanged) and really many others which are different in both tables. I wish to get latest X updates from both tables (in future from more tables) but I cannot find the way to do it. I mean something like
SELECT `name`, `dateChanged` FROM `mpolymer`, `mmetal` ORDER BY `dateChanged` DESC LIMIT 0,10
to show quick summary of newest activities. I tried UNION, JOIN, but all of these need to join on some common column, which is not possible (I think). Do you have any possibilities to do what I wish to do, please?
Assuming that you want the last X events across both tables (not X events from one and X events from the other), this should work for you.
SELECT name, dateChanged FROM mpolymer
UNION
SELECT name, dateChanged FROM mmetal
ORDER BY dateChanged DESC limit 10;
DEMO
To also show the table name (based on your comment)…
SELECT name, dateChanged, 'mpolymer' AS tableName FROM mpolymer
UNION
SELECT name, dateChanged, 'mmetal' AS tableName FROM mmetal
ORDER BY dateChanged DESC limit 10;
DEMO

Whats wrong with this MYSQL query

I have the following SQL query , it seems to run ok , but i am concerned as my site grows it may not perform as expected ,I would like some feeback as to how effective and efficient this query really is:
select * from articles where category_id=XX AND city_id=XXX GROUP BY user_id ORDER BY created_date DESC LIMIT 10;
Basically what i am trying to achieve - is to get the newest articles by created_date limited to 10 , articles must only be selected if the following criteria are met :
City ID must equal the given value
Category ID must equal the given value
Only one article per user must be returned
Articles must be sorted by date and only the top 10 latest articles must be returned
You've got a GROUP BY clause which only contains one column, but you are pulling all the columns there are without aggregating them. Do you realise that the values returned for the columns not specified in GROUP BY and not aggregated are not guaranteed?
You are also referencing such a column in the ORDER BY clause. Since the values of that column aren't guaranteed, you have no guarantee what rows are going to be returned with subsequent invocations of this script even in the absence of changes to the underlying table.
So, I would at least change the ORDER BY clause to something like this:
ORDER BY MAX(created_date)
or this:
ORDER BY MIN(created_date)
some potential improvements (for best query performance):
make sure you have an index on all columns you querynote: check if you really need an index on all columns because this has a negative performance when the BD has to build the index. -> for more details take a look here: http://dev.mysql.com/doc/refman/5.1/en/optimization-indexes.html
SELECT * would select all columns of the table. SELECT only the ones you really require...

Select most frequent from column in

Hi i have a columm in the table and i want to select the most common item from the selected column. The table is set up
publication:
id
Title
published
I want to beable to select the most recurring places where publications have been published. Is this possible to do?
Thanks in Advance
Dean
select published, count(*) nbr
from table1
group by published
order by nbr desc
limit 1
You don't really need the count, but if you wanted confirmation that the choice seemed reasonable, you could use it. Also, you didn't specifically say whether you wanted ONLY the one, or wanted to see which was the most frequent, along with frequencies of the other records. Take off the limit 1 if you want to see all records.