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
Related
I want to count all rows that infront of a specific ID, so I can output how many entries are infront of the users entry.
I have no clue how to count the rows, starting at the ID of the user (for ex. ID 10).
How to count from ID 10 to ID 1 where status = unpublished
Thx
Are you looking for something like-
SELECT COUNT(*)
FROM your_table
WHERE ID BETWEEN 1 AND 10
AND STATUS = 'unpublished'
Try This --
SELECT COUNT(*)
FROM tablename
WHERE ID BETWEEN 1 AND 10
AND STATUS = 'unpublished'
ORDER by ID DESC
SELECT * FROM
WHERE ID = 10 AND status='unpublished';
I have these records in a MySQL DB. On a JSON type column.
Record1: {"images": ["image.jpeg", "image.jpeg"]}
Record2: {"images": ["image.jpeg", "image.jpeg", "image.jpeg"]}
Record3: {"images": ["image.jpeg"]}
How can I get the total count of images by the key name of the JSON property which in my case is 'images'?
I am trying to achieve the same thing as running this below query, but counting the array items from the images key.
SELECT
`field`,
count( * ) AS total
FROM
`table_name`
WHERE
`condition`
GROUP BY
`something`
which will give you this result:
field total
------------------
field1 5
field2 2
What I am trying to achieve:
field total
-----------------
images 6
Table structure
Table data
Try:
SELECT
`type`,
SUM(
JSON_LENGTH(`media`, '$.images')
) `media`
FROM
`table_name`
GROUP BY
`type`;
See dbfiddle.
ok sure You can use this query :
SELECT type,JSON_LENGTH(media) FROM `tabl4` group by type
check the execution from here : Demo
I have a mysql table with multiple columns. The primary key of the table is the 'id' column. A row has multiple columns but the most relevant one for this question is 'Date' which is basically a timestamp. What is an efficient way to get the order (rank) of a given row id if I want to order the rows by their timestamp. The most recent timestamp is of rank 1, the second is of rank 2 and so on. I want to return the rank of a given row
Edit: I use ORDER BY to get an ordered set but I want the mysql statement to return the order of the specific item, not an ordered rows. I also don't want to parse the result set since this is very time cosnuming
Edit2: for example assume the following table
id timestamp name
1 Dec 4, 2016 Bob
2 Jan 1, 2015 Eve
3 Feb 6, 2017 Alice
Given an id, I should return the order of the item
id=1, expected output: 2
id=2, expected output: 3 (least recent)
id=3, expected output: 1 (most recent)
answer with out edit :Use row_number () over (order by ) clause and put some *dummy
select ROW_NUMBER()over(order by (select1) ) as renk ,* from table
answer after edit in question..
select ROW_NUMBER()over(order by timestamp desc ) as renk ,* from table
Hi try this in mysql
SET #rank=0;
SELECT t.rank FROM
(SELECT #rank:=#rank+1 AS rank, id
FROM `table_name`
ORDER BY timestamp DESC) t WHERE t.id=5;
selct * from table where id>1 order by id asc limit 1
if( not date ){
selct * from table where id>0 order by id asc limit 1
}
I have page where people can post comments and page where people can click "follow" on other people profiles (same as LIKE on Facebook)
I would like to have SELECT query that will post all the comments I have, but will order them with the follow way:
First, print the 2 newest comments (they must been posted this week) of the lastest people you click FOLLOW.
Second, post the rest of the posted, order them by create-date
(I'm using linux time)
Can you help me with the SQL query?
This is my current SELECT query. it pull all comment by create-date:
SELECT
id, userID, text, createDate
FROM `comments` AS comment
WHERE (comment.refID = 0)
AND (comment.pageName = 'yard')
AND 1=1
ORDER BY comment.createDate DESC LIMIT 0, 20
"followers" table looks like this:
userID ownerID createDate
1 2 1439019657
1 4 1438940399
(user 1 is following after user 2 and 4)
"comments" table looks loke this:
id userID pageName refID text createDate
220 1 yard 0 text1 1438030967
227 1 yard 0 text2 1438031704
228 1 yard 0 text3 1438031704
(userID - which user publish the comment. refID - always "0". pageName - always "yard")
So is this case, if I'm user number 1, than I would like to see the newest 2 comments of users 2 and 4 (only if they where made in the last week) and than to see all the rest of the comments (of all users) order by date (without , of course, the once that I already saw)
You are going to have to split this up into 2 queries.. ..and I am guessing at some of the table relationships here..
// get the last 2 comments from followed users..
SELECT *
FROM comments
WHERE userID IN (
SELECT ownerID
FROM followers
WHERE userID = ?
) ORDER BY createDate DESC
LIMIT 2
// then get the rest, removing already returned comments
SELECT *
FROM comments
WHERE id NOT IN (
-- either put the above query in here
-- or buuild an array of the 'id's when outputting the results above
-- and use that in here to prevent them being returned again
) ORDER BY createDate DESC
(select com.userID,com.page,com.text
from followers as fol
JOIN comments as com
ON fol.ownerId=com.userId
where
(
from_unixtime(com.createDate) BETWEEN
(
UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL -14 DAY) AND UNIX_TIMESTAMP(NOW())
)
Order BY com.createDate desc
)
Union
(select com.userID,com.page,com.text
from comments as com
where com.id NOT In
(select com.id
from followers as fol
JOIN comments as com
ON fol.userId=com.userId
where
(
from_unixtime(com.createDate) BETWEEN
(
UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL -14 DAY) AND UNIX_TIMESTAMP(NOW())
)
)
Order BY com.createDate desc
)
Explanation:
There are two queries merged in one using union.
Select statement will give all the data follower comments.
this is accomplished using Join.
Select statement to get all the other comments then the comments of the above 1st query.
the only thing that is not clear is how you are deciding whether the
comments is been read by the user (Insufficient information)
I have a table like this:
`id|value
1|2
2|8
3|5
4|6
5|10
6|7`
I need a query to pull AND sum the 3 highest values. So the correct query would pull the following:
3 highest:
5|10
2|8
6|7
Sum of 3 highest values = 25
I feel like this should be pretty simple but i'm having a tough time! Thanks for your help
SELECT SUM(Value) AS SumOfTop3Values
FROM (
SELECT Value
FROM Table
ORDER BY Value DESC
LIMIT 3
) AS sub
I think you need to wrap this in a subquery:
SELECT SUM(value) AS total FROM (
SELECT value FROM table
ORDER BY value DESC
LIMIT 3
);
To have MySQL return highest 3 values and their Sum in a 4th row, you can use (aasuming that id is the Primary Key of the table):
SELECT id, SUM(value)
FROM
( SELECT id, value
FROM TableX
ORDER BY value DESC
LIMIT 3
) AS tmp
GROUP BY id
WITH ROLLUP ;