Get items count by key name - php

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

Related

MySQL return one occurrence of duplicate values

Trying to figure out if there is a MySQL query for the following scenario:
Database example
id---fruit------date
01---apple------1990-01-01
02---banana-----2020-01-01
03---orange-----2021-02-03
04---apple------1999-12-12
05---pear-------2009-01-02
06---apple------2008-09-18
07---banana-----2007-11-12
I want a query to return all rows but for rows with the same fruit only one occurence and preferably the occurence with the latest date. So based on the above the query would return:
id---fruit------date
02---banana-----2020-01-01
03---orange-----2021-02-03
05---pear-------2009-01-02
06---apple------2008-09-18
SELECT * FROM `table_name`
WHERE
id
IN
(
SELECT SUBSTRING_INDEX(temp,"|",1) `id` FROM
(
SELECT *,GROUP_CONCAT(id ORDER BY DATE DESC SEPARATOR "|") temp FROM `table_name`
GROUP BY fruit
)
AS dbx
)
Explaination:
Group_concat Id with Order by date. At this time I user separator "|"
Get position of that char to return latest ID
Select again from that table

Mysql query to retrieve single image from an array of images?

Table to store array of Images:
`products` and `products_photos`
`products` includes only `id` field
`products_photos` includes `product_id` and `filename`
Table values:
products
--------
id-6
id-7
products_photos
---------------
6 - filename1
6 - filename2
7 - filename3
In these two table id 6 includes 2 images but i need to fetch only one image from that. How to write a query for that in mysql or laravel.
If you want to get the latest (last added) image of the product then you can do the following way
NOTE : Instead of *, use the precise columns so that your query performs faster.
SELECT * FROM products_photos WHERE product_id = 6 ORDER BY products_photos.id DESC LIMIT 1;
If you want to get the first (old added) image of the product then you can do the following way
SELECT * FROM products_photos WHERE product_id = 6 ORDER BY products_photos.id ASC LIMIT 1;
Updated : Query to get the list of all the records group
SELECT MAX(id) as id, photo_name FROM products_photos GROUP BY photo_name
In case if you have more than one columns and want to get the latest record for that too then go for this
Eg. Considering that your having description column and want to get the MAX (latest) content than use MAX, if you need oldest use MIN
SELECT MAX(id) as id, photo_name, MAX(description) as description FROM products_photos GROUP BY photo_name
You can give limit to your query to get single result
SELECT * FROM products_photos WHERE product_id = 6 ORDER BY id LIMIT 1;
Which will give single image only for id = 6 product
For retrieve all data use group by
SELECT * FROM GemSellerPortal.products pr
LEFT JOIN GemSellerPortal.products_photos ph
ON ph.product_id = pr.id
GROUP BY pr.id

Select distinct column from table

I have the database structure as shown in the below image:
I want to retrieve variant_id that belongs to particular product_id
Example :
lets say 1 and Size = L , Color = Green.
I expect mysql query to return variant_id = 7.
What is the expected query to be used in this scenario?
You can use following query.
I'm assuming your table name is table1
select variant_id from (select * from table1 where
product_id=1 and ((attribute_name='Size' and value='L')
or (attribute_name='Color' and value='Green'))) as temp
group by variant_id having count(*)>1

Counting multiple rows in Mysql

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

select the 3 highest values using php mysql query

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 ;

Categories