简体   繁体   English

限制和抵消左联接的搜索查询

[英]Limit and offset a search query that has a left join

I've got two tables: tasks and pages. 我有两个表:任务和页面。 The first page has a column that references the second table multiple times, called page_number . 第一页上有一列多次引用第二张表,称为page_number I want to get all fields of the first table and all page number values for the same tasks.id . 我想获取第一个表的所有字段以及相同task.id的所有页码值。 My query for getting this data is as such: 我查询此数据是这样的:

SELECT
TASKS.ID,TASKS.URL,TASKS.ASSIGNEE,PAGES.TASK_ID,PAGES.PAGE_NUMBER
FROM TASKS
INNER JOIN (SELECT * FROM TASKS ORDER BY TASKS.ID LIMIT ? OFFSET ?)
AS T ON (TASKS.ID=T.ID)
LEFT JOIN PAGES ON (TASKS.ID=PAGES.TASK_ID);

This works, and I get an output like so: 这可行,并且我得到如下输出:

      id  |          url          | assignee | task_id | page_number
------+-----------------------+----------+---------+-------------
   15 | /vector.pdf           |          |      15 |           1
   15 | /vector.pdf           |          |      15 |           2
   23 | /raster.pdf           |          |      23 |           1
   23 | /raster.pdf           |          |      23 |           2
   23 | /raster.pdf           |          |      23 |           4
 1001 | https://everette.com  |          |    1001 |           1
 1001 | https://everette.com  |          |    1001 |           2
 1002 | https://scarlett.com  |          |    1002 |           1
 1002 | https://scarlett.com  |          |    1002 |           2
 1002 | https://scarlett.com  |          |    1002 |           3
 1002 | https://scarlett.com  |          |    1002 |           4
 1002 | https://scarlett.com  |          |    1002 |           5
 1002 | https://scarlett.com  |          |    1002 |           6
 1002 | https://scarlett.com  |          |    1002 |           7

But I've come upon other requirements: I need to get a total count of the TASKS table as well in the same query (for pagination in the front-end), and I need to be able to search the resulting table (ie look for any substring in the table) . 但是我还有其他要求:我需要在同一查询中获得TASKS表的总数,(在前端进行分页),并且我需要能够搜索结果表(例如表中的任何子字符串)。 To search the table, I did this: 为了搜索表格,我这样做:

SELECT 
TASKS.ID,TASKS.URL,TASKS.ASSIGNEE,PAGES.TASK_ID,PAGES.PAGE_NUMBER
FROM TASKS
INNER JOIN (SELECT * FROM TASKS ORDER BY TASKS.ID LIMIT ? OFFSET ?)
AS T ON (TASKS.ID=T.ID)
LEFT JOIN PAGES ON (TASKS.ID=PAGES.TASK_ID)
WHERE TASKS.URL  LIKE ?
OR CAST(TASKS.ID AS TEXT) LIKE ?

I do get results, but LIMIT and OFFSET work on the joined table before the WHERE clause, so I set LIMIT to something like 10 and OFFSET to 5, try to look for a record that's in the first 5 records and get no results because the matching records are further on in the original table: 我确实得到了结果,但是LIMIT和OFFSET在WHERE子句之前的连接表上起作用,因此我将LIMIT设置为10,将OFFSET设置为5,尝试查找前5个记录中的记录,但没有结果,因为匹配的记录将在原始表中进一步显示:

SELECT
TASKS.ID,TASKS.URL,TASKS.ASSIGNEE,PAGES.TASK_ID,PAGES.PAGE_NUMBER
FROM TASKS
INNER JOIN (SELECT * FROM TASKS ORDER BY TASKS.ID LIMIT 10 OFFSET 5)
AS T ON (TASKS.ID=T.ID)
LEFT JOIN PAGES ON (TASKS.ID=PAGES.TASK_ID)
WHERE TASKS.URL  LIKE '%everette%'
OR CAST(TASKS.ID AS TEXT) LIKE 99

Actual output: 实际输出:

 id | url | assignee | task_id | page_number
----+-----+----------+---------+-------------
(0 rows)

Expected Output: 预期产量:

id  |          url          | assignee | task_id | page_number
------+-----------------------+----------+---------+-------------
1001 | https://everette.com  |          |    1001 |           1
1001 | https://everette.com  |          |    1001 |           2

How should I structure this query so I get all records that match the WHERE clause, getting at most limit tasks? 我应该如何构造此查询,以便获得与WHERE子句匹配的所有记录,最多获得限制任务?

I'm using PostgreSQL, btw. 我正在使用PostgreSQL,顺便说一句。 Thanks in advance for any help. 在此先感谢您的帮助。

You could add that count to the column list: 您可以将该计数添加到列列表中:

SELECT ... column list ..., 
       (select count(*) from tasks) as total_task
FROM tasks
  JOIN (SELECT * FROM tasks ORDER BY tasks.id LIMIT ? OFFSET ?) AS T 
    ON tasks.id = t.id
  LEFT JOIN pages ON tasks.id pages.task_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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