MySql:从组中选择第一行,两个表之间使用INNER JOIN

时间:2017-08-16 18:52:21

标签: php mysql join

要为我的问题提供更多信息,请修改插入的图片。

https://i.stack.imgur.com/1IuID.jpg

SELECT pro.id
     , pro.featured
     , pro.price_per_night
     , pro.meta_title
     , img.image
  FROM properties AS pro
  INNER JOIN images AS img 
    ON pro.id = img.imageable_id
 WHERE pro.featured = 1

使用此查询,将显示符合查询的所有图像。也就是说,显示图像:image4,image5,image6,image7,image8和image9。

例如,如果我订购LIMIT = 1,则只显示一张图片,更准确地说是" image4"。不再显示图像。

我想更改查询以显示与查询匹配的字段的第一个图像。更确切地说,我必须展示" image4"和" image7"。

再次感谢您的帮助

2 个答案:

答案 0 :(得分:1)

mysql limit

SELECT pro.id, pro.featured, pro.price_per_night, pro.meta_title, img.image
FROM properties AS pro 
INNER JOIN images AS img ON pro.id = img.imageable_id 
WHERE pro.featured = 1
LIMIT 1

答案 1 :(得分:0)

您需要拥有图片ID并首先使用图片FROM properties AS pro

JOIN images AS img

ON pro.id = img.imageable_id

WHERE image_id=1

#!/bin/bash

# Register signal handler for SIGINT (Ctrl+c)
trap abort INT 

function abort() {
    echo "Sending SIGINT to background process ${pid}"
    # Kill background process
    kill "${pid}"
    # Wait for it to finish after killing it
    wait "${pid}"
    # Exit the script
    echo "Aborting"
    exit 1   
}

# Start long running process in background (&)
sleep 1000 &
# Obtain the pid of that process
pid=$!
# Wait for the background process to finish
wait

read