简体   繁体   中英

MYSQL nested select query with *

I have a table called incidents and it has the following fields:

`index`, `timestamp`, `refNum`, `priority`, `status`, `type`, `email`, `telephone`, `title`, `description`, `system`, `category`, `time`, `requiredBy`, `dateReason`, `oneOff`, `url`, `browserDevice`, `jsessionid`, `customersProducts`, `investigationsUndertaken`, `whatquestions`, `supportingInformation`, `open`

I am quite new to trying to nest a query and I am trying to select all records in my incidents table where the title or status match a keyword and then I only want the results which occur between a selected timestamp range. Both the individual queries here work on their own and return results but when I try and nest them I get the error #1241 - Operand should contain 1 column(s). What am I doing wrong?

SELECT
    *
FROM
    `incidents`
WHERE
     `timestamp` BETWEEN '2014-12-01 00:00:00' AND '2015-11-23 23:59:59'  
IN
(SELECT * FROM `incidents` WHERE `title` LIKE '%test%' OR `status` LIKE '%test%')

Ideally I want to return all fields, hence the use of *, where it's between my timestamp range and the status or title contain a keyword I specify. In the example query above I have used the keyword 'test'.

When you write a subquery, it's generally expected that you will provide an index column for the main query to return from. You cannot return SELECT * because there's no implicit column to key off of. I picked index (hopefully that's actually an index)

SELECT
    *
FROM
    `incidents`
WHERE
     `timestamp` BETWEEN '2014-12-01 00:00:00' AND '2015-11-23 23:59:59'  
AND index IN
(SELECT index FROM `incidents` WHERE `title` LIKE '%test%' OR `status` LIKE '%test%')

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