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

user8268872

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.

Channaveer Hakari

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel: getting a a single value from a MySQL query

From Dev

Generate single-dimensional array with MySQL query

From Dev

How to retrieve image file paths from mysql database and display the images using php

From Dev

How to retrieve values stored in JSON array in MySQL query itself?

From Dev

how can i retrieve data from multiple databases in a single query?

From Dev

Mysql: fetch value from database and php array in single query

From Dev

php code to retrieve image from /images

From Dev

How to retrieve min price via mysql query from a joined table

From Dev

Create a single image from images array

From Dev

Foreach mysql query from array, then a second query from that first array

From Dev

MySQL - Query a temp table to retrieve 2 rows from table

From Dev

need multiple outputs from single query - mysql

From Dev

Adding multiple images on single image (with data from database)

From Dev

Download images from URL and add to array as image

From Dev

combine an array of images to a single image which is a row of images in swift

From Dev

How to retrieve Image from MySQL database using C#

From Dev

python PIL acces multiple images from a single image file

From Dev

mysql query to retrieve every single row of data

From Dev

How to retrieve image file paths from mysql database and display the images using php

From Dev

Mysql: fetch value from database and php array in single query

From Dev

MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

From Dev

run a query code to retrieve array from database without page refresh

From Dev

Sorting Array from a MySql Query

From Dev

Foreach mysql query from array, then a second query from that first array

From Dev

Show single record from mysql query database

From Dev

Query to retrieve stock quotes variation from a single day

From Dev

How i can retrieve this array of images from json?

From Dev

Codeigniter return mysql query result to single array

From Dev

How to retrieve image blob from MySQL into C# application?

Related Related

  1. 1

    Laravel: getting a a single value from a MySQL query

  2. 2

    Generate single-dimensional array with MySQL query

  3. 3

    How to retrieve image file paths from mysql database and display the images using php

  4. 4

    How to retrieve values stored in JSON array in MySQL query itself?

  5. 5

    how can i retrieve data from multiple databases in a single query?

  6. 6

    Mysql: fetch value from database and php array in single query

  7. 7

    php code to retrieve image from /images

  8. 8

    How to retrieve min price via mysql query from a joined table

  9. 9

    Create a single image from images array

  10. 10

    Foreach mysql query from array, then a second query from that first array

  11. 11

    MySQL - Query a temp table to retrieve 2 rows from table

  12. 12

    need multiple outputs from single query - mysql

  13. 13

    Adding multiple images on single image (with data from database)

  14. 14

    Download images from URL and add to array as image

  15. 15

    combine an array of images to a single image which is a row of images in swift

  16. 16

    How to retrieve Image from MySQL database using C#

  17. 17

    python PIL acces multiple images from a single image file

  18. 18

    mysql query to retrieve every single row of data

  19. 19

    How to retrieve image file paths from mysql database and display the images using php

  20. 20

    Mysql: fetch value from database and php array in single query

  21. 21

    MYSQL Single query to retrieve both single row from one table and many rows as a single field from another

  22. 22

    run a query code to retrieve array from database without page refresh

  23. 23

    Sorting Array from a MySql Query

  24. 24

    Foreach mysql query from array, then a second query from that first array

  25. 25

    Show single record from mysql query database

  26. 26

    Query to retrieve stock quotes variation from a single day

  27. 27

    How i can retrieve this array of images from json?

  28. 28

    Codeigniter return mysql query result to single array

  29. 29

    How to retrieve image blob from MySQL into C# application?

HotTag

Archive