I have mysql table posts
Posts
PostId (int)
PostTitle (varchar)
PostContent (text)
PostHits (int)
PostStatus (varchar)
and I read these records on select query like
SELECT * FROM posts WHERE PostStatus LIKE 'ALIVE';
is it possible that all records increment their value of PostHits on each select if they qualify where clause? if yes what is the best possible way, currently I am doing it using two queries select and then update based on where id in(all selected posts ids). I am looking for single query solution.
Is this what you need?
UPDATE posts
SET PostHits = PostHits + 1
WHERE PostStatus LIKE 'ALIVE'
Using SELECT only
SELECT
PostId,
PostTitle,
PostContent,
PostHits + 1 AS PostHits,
PostStatus
FROM posts
WHERE PostStatus LIKE 'ALIVE'
Related
Actually I have a table which name is posts and another table which name is newscount , in posts table there is four column id , title ,details, postingDate
and in newscount table there is three column which are id, postid, count ,
and in newscount table postid is the id of posts table id , I have saved data in posts table and also i have data in newscount table , for example - in posts table I have
id title details postingDate
1,title1,details1,somedate1
2,title2,details2,somedate2
3,title3,details3,somedate3
and in newscount table I have
id=1,postid=1,count=6
id=2,postid=2,count=5
id=3,postid=3,count=7
I want to select last two data from posts table which have maximum count , like if i select then it should show result like newscount.id=3,posts.id=3 which has max 7 count and newcount.id=1,posts.id=1 and its details
I have tried but it is showing only postsid 3detials not last two
SELECT * FROM (SELECT newscount.postId, newscount.count from newscount WHERE newscount.count = (SELECT max(count) FROM newscount ORDER BY id LIMIT 2) ) tempcounts INNER JOIN posts ON posts.id = tempcounts.postId WHERE posts.isActive=1 AND posts.postingDate >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND posts.postingDate < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY ORDER BY posts.postingDate DESC limit 2
I couldn't run your query because you didn't give a complete table structure of your db, but you have at least 2 item to make them correct:
first:
you have this query, but it does not return two parameters(except in condition you have more than one row with max value)
(SELECT max(count) FROM newscount ORDER BY id LIMIT 2)
instead of that use this:
(SELECT count FROM newscount ORDER BY count DESC LIMIT 2)
the second: if you expect to get more than 1 row from a subquery in where clause you should use in instead of = operand
So i have this query to fetch all posts
select id, title
from posts
where type = 'article'
and i have a stored function that calculate the total of views and return the result
i know i can execute it like
select totalView(id)
but how can i merge the two queries to return all posts with the total view of each post
maybe something like
select id, title, totalView(id) as total
from posts
where type = 'article'
Thank you.
select count(*) as total, id, title from posts where type = 'article'
EDIT : It will count all the rows of $id
select count(*) as total, id, title from posts where type = 'article' AND id = $id;
I wanna select from 2 tables.
In first table I want select all data.
In second table I want select only "name".
'edition' in first table it's 'id' in second table.
I wrote sql code, but it isn't work:
SELECT link, title, description, imgUri, vkCount, fbCount, twCount, edition
FROM articles
RIGHT JOIN SELECT name
FROM editions
WHERE id = articles.edition
ORDER BY (vkCount + fbCount + twCount) DESC
LIMIT 0, $count
Structure: http://i.imgur.com/bMcokCp.png
Looks pretty close but you have the JOIN syntax a little wrong. Use ON for a join and only one SELECT clause
SELECT link, title, description, imgUri, vkCount, fbCount, twCount, edition, name
FROM articles
JOIN editions ON id = articles.edition
ORDER BY (vkCount + fbCount + twCount) DESC
LIMIT 0, $count
I want to make a fast table structure for news site with php and mysql. My database structure is ID, title, content, cat_ids (; separedet IDs of categories - ;5;10;15;20;), active, publish_date.
I want to make a fast query to select news from this table. Something like that:
SELECT id
FROM news
WHERE cat_ids LIKE '%;15;%'
AND active = 1
AND publish_date < NOW()
ORDER by publish_date DESC
LIMIT 0, 10
But if my table is 2-3GB the query is very slow. I need some ideas to make structure and make the select faster.
Instead of using cat_ids column, try creating a news_cats table with news_id and cat_id, and using this query:
SELECT id
FROM news JOIN news_cats ON news_id = id
WHERE cat_id = 15
AND active = 1
AND publish_date < NOW()
ORDER by publish_date DESC
LIMIT 0, 10
Some suggestions as below:
1) Create index on "active" field
2) Create index on "publish_date" field
3) Create separate table for category and news relation and remove "cat_ids" field from news table
New table might look like below:
news_category_ids
news_id
category_id
It can have multiple rows for each news_id, if news item falls in 3 categories, it will have 3 rows
Then use SQL like below:
SELECT news.id
FROM news INNER JOIN news_category_ids ON news.id = news_category_ids.news_id
WHERE 1
AND news.active = 1
AND news_category_ids.cat_id = 15
AND news.publish_date < NOW()
ORDER by news.publish_date DESC
LIMIT 0, 10
I'm currently having a table filled with likes and tweets of a certain post sorted on date.
I want to know the query to count the total of likes and tweets sorted by post_id. The result of the example below should be 50 likes and 20 tweets.
The structure of the table is:
post_id date likes tweets
1 2012-06-09 20 10
1 2012-06-10 30 10
Have a look at this doc.
Try this:
SELECT SUM(`likes`) AS `likes`, SUM(`tweets`) AS `tweets` FROM `table` GROUP BY `post_id`
In your case:
your table name assumed: tweets
select SUM(likes), SUM(tweet) from post_table group by(post_id)
General Form:
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name