简体   繁体   中英

Merging results from MySQL Query

I will try and set the scene as best i can. *lights candle

What i am trying to acheive is this

  1. Search image_likes to see if the user has liked any images and then include them in next query.
  2. Grab all images from images that user has uploaded.
  3. Show both results aswell as ORDER BY id DESC.

I am horrible at MySQL when it comes to including different table results, so i hope i have included enough information. Let me know if you need anymore.

Table Structures

image_likes

id | user_id | image_id

images

id | uploader_id | date

you should really try to get a grasp on JOINs. you will continually write bad code, and design bad databases to work around not knowing how to use them.

SELECT *
FROM images
LEFT JOIN image_likes ON image_likes.image_id = images.id
WHERE image_likes.user_id = $my_id
OR images.creator_id = $my_id
ORDER BY images.id DESC

1:

SELECT * FROM Image_Likes WHERE User_Id = x ORDER BY Id DESC 

2:

SELECT * FROM Images WHERE Uploader_Id = x ORDER BY Id DESC

3: (all together)

SELECT L.*, I.* FROM Image_Likes L LEFT JOIN Images I ON L.Image_id = I.Id WHERE L.User_ID = x  ORDER BY I.Id DESC 

Replace x with the User's ID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM