简体   繁体   中英

MySql limiting only first table from joined table

I'm kind of struggling to limit only one table from the joined table.

I want to limit only the first table rosary_group_name. In the code below I limit both tables. Any idea?

$sql = "
    SELECT e.id,
           e.rosary,
           e.group_name,
           e.description,
           u.group_id,
           u.name,
           u.decade,
           u.intention,
           u.datume 
      FROM `rosary_group_name` AS e 
INNER JOIN `rosary_groups` AS u 
        ON e.id = u.group_id 
  ORDER BY e.id DESC $limit;
"; 

the variable $limit sets the number of rows to display from both tables. I want to apply $limit only to the first table.

You can try to use a sub-query for the first table, like

SELECT 
       e.*
       u.group_id,
       u.name,
       u.decade,
       u.intention,
       u.datume 
FROM (SELECT e.id, e.rosary, e.group_name, e.description, FROM `rosary_group_name` AS e $limit) AS e 
INNER JOIN `rosary_groups` AS u 
        ON e.id = u.group_id 
ORDER BY e.id DESC;

(not sure if the syntax is correct, sorry. but I hope you get an idea)

Inner join will give you limited(matched rows) result. You should consider left join in order to get more result from the left side table

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