473,734 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to specify which table to LIMIT in a LEFT JOIN?

103 New Member

I'm having a bit of trouble with a query. I am trying to paginate a set of results using LIMIT. Each result has a bit of 'basic' data and potentially multiple 'additional' data. I have used LEFT JOIN to get the data from table 2.

I want to limit the amount of data to 10 rows of table 1 but if there are two pieces of data on table 2 for every row on table 1, I only get 5 rows of table one (ie 10 rows in all but not what I want). A GROUP BY clause seemed to prevent the LEFT JOIN from working although did force the query to return 10 rows of table1.

How can I apply the limit specifically to table 1? Or perhaps I can use a function in the LIMIT clause to calculate the correct numbers on the fly?

Here is the SQL (I use MySQL 5).

Expand|Select|Wrap|Line Numbers
  1. SELECT table1.colA , table1.colB, table1.colC, table2.colX, table2.colZ
  2. FROM table1
  3.   LEFT JOIN table2 ON table1.id=table2.id
  4. LIMIT 0,10;
Thanks so much. It will be really great to solve this problem!

Aug 10 '07 #1
1 22734
5,821 Recognized Expert Expert
Heya, Henry.

To accomplish this, you need to create derived tables from subqueries:
Expand|Select|Wrap|Line Numbers
  2.         `t1_master`.`colA`,
  3.         `t1_master`.`colB`,
  4.         `t1_master`.`colC`,
  5.         `t2_master`.`colX`,
  6.         `t2_master`.`colZ`
  7.     FROM
  8.     (
  9.         (
  10.             SELECT
  11.                     *
  12.                 FROM
  13.                 (
  14.                     SELECT
  15.                             *
  16.                         FROM
  17.                             `table1`
  18.                         LIMIT 0,10
  19.                 )
  20.                     AS `t1_sub`
  21.         )
  22.             AS `t1_master`
  23.         LEFT JOIN
  24.         (
  25.             SELECT
  26.                     *
  27.                 FROM
  28.                 (
  29.                     SELECT
  30.                             *
  31.                         FROM
  32.                             `table2`
  33.                 )
  34.                     AS `t2_sub`
  35.         )
  36.             AS `t2_master`
  37.             USING
  38.                 (`id`)
  39.     )
Aug 14 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Stephen Patten | last post by:
Hi All, While in the process of building my table (40 or so Insert statments) can I then query ("select * from @Table_variable") and use the results up to theat point for another insert into @Table_varible? If you look for stepID -15 I have commented that section out due to it not retuning the correct values. Thank you in advance
by: Andrew | last post by:
Hello, Rather complicated one im afraid.. (well for me anyway!) Background on this one is that I have inherited a system that needs some modification, have some basic sql knowledge but fear i've bitten off a little bit more than I can handle. I need to export some data which I am able to do as I have some exporting software which can just tap into a mysql database. Its a fairly straightforward real estate database.
by: elein | last post by:
Yes, I vacuumed. Reproduced on both 7.3.2 and 7.5. Brain dead java beans want order by clauses in views that they use. my view is: select .... from bigtable b left join lookuptable l order by bigkey desc;
by: aaron.reese | last post by:
Guys, this is my problem:- I have records which are linked by two fields on a left inner join (there may be 0,1 or more records in the right hand table) The relationship is not unique (it's a map reference so it can't be...) so I am getting a cartesian join on the right hand table - look, it's not my database OK, I don't have any choice!!!
by: dmonroe | last post by:
hi group -- Im having a nested inner join problem with an Access SQl statement/Query design. Im running the query from ASP and not usng the access interface at all. Here's the tables: tblEmployees empId -- EmpName -- EmpRole -- EmpManager -------....------------.... ---------....--------------- 1........ dan yella..........1..........2
by: angelnjj | last post by:
I'm going to do my best to describe purpose and what I need...here goes. I'm writing a data entry "quality" report and working. I'm trying to identify anytime a user adds a new contact to the db during a date range that already existed within the account prior to the first date of the range. When the contact existed already, the returned value should be 1, if they didn't exist the returned value should be 0 (I'll then total up all the 1s to...
by: denjermen | last post by:
If anybody can help me with this I will be forever grateful. I'm no programmer nor SQL expert, but weekly I have to run a report compiling data from several tables into an excel spreadsheet. I figured I could just write a simple SQL to do that for me. Man, was I wrong. Here is what I have: SELECT re_prop.glc, pm_master.projid, re_prop.addr1, re_prop.city, re_prop.state, re_prop.puse, re_disposition.source_list, pm_master.projtype,...
by: perryche | last post by:
I am not sure if I am asking the right question in the subject here. But, here is my problem. I have 5tables/queries with various data like below. Table1: CustomerID1, Field1, Field2 Table2: CustomerID1, Field5, Field6 Table3: CustomerID2, Field7, Field8 (customerID1 does not exist in the table) ....
by: hinksta | last post by:
I have a query that has two tables with a join and works fine, the tables are from a vbulletin database and the query is from an old unsupported mod. I’m trying to add a third table to the query, I’m a bit new to joins and having a bit of trouble getting it to work. This is the original query $NewestThreads = $vbdb->query(" select t.*,p.pagetext from ".TABLE_PREFIX."thread t left join ".TABLE_PREFIX."post p on(p.postid=t.firstpostid)...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.