Help creating an SQL query

A few new SQL queries, and I need a little help with my connection.

I am provided with gid

For each of them I need to grab a name from the wp_ngg_gallery table

then join the wp_ngg_pictures table and take the file_name field limit 1 DESC by the field imagedate

alt text http://cl.ly/80c22b7010ab8f021558/content

alt text http://cl.ly/bfdf41b3706d25e0ee10/content

Anyone who can help?

+3
source share
3 answers

I'm going to guess:

select wp_ngg_gallery.name, wp_ngg_pictures.filename
from wp_ngg_gallery, wp_ngg_pictures
where wp_ngg_pictures.galleryid = wp_ngg_gallery.gid
order by wp_ngg_pictures.imagedate DESC 
limit 0,1

GID, :

select wp_ngg_gallery.name, wp_ngg_pictures.filename
from wp_ngg_gallery, wp_ngg_pictures
where wp_ngg_pictures.galleryid = wp_ngg_gallery.gid
and wp_ngg_gallery.gid = $gid
order by wp_ngg_pictures.imagedate DESC 
limit 0,1

($ gid , , ​​PHP, , , , )

, wp_ngg_pictures.galleryid wp_ngg_gallery.gid, , . ( ), ( ), and wp_ngg_gallery.gid = $gid.

0

, . ( phpMyAdmin) SQL, ( , ).

SELECT
    `wp_ngg_gallery`.`name`,
    `wp_ngg_pictures`.`filename`
FROM
    `wp_ngg_gallery`
    LEFT JOIN
        `wp_ngg_pictures`
    ON
        `wp_ngg_gallery`.`gid` = `wp_ngg_pictures`.`galleryid`
ORDER BY
    `wp_ngg_pictures`.`imagedate` DESC
LIMIT
    0,1;

, JOIN. , GROUP BY.

+3

gids ( ):

SELECT gid,name, a.filename
FROM wp_ngg_gallery
JOIN  wp_ngg_pictures a
ON a.galleryid = gid 
LEFT JOIN wp_ngg_pictures b
ON a.galleryid = gid 
AND b.imagedate > a.imagedate
WHERE b.galleryid IS NULL
AND gid IN (1,2,3)
0

Source: https://habr.com/ru/post/1748906/


All Articles