1

I have a query as follows:

SELECT event 
FROM log
WHERE user = (SELECT SUBSTRING(details,55) FROM log WHERE details LIKE 'ID: 308%')

I know I can use an inner join or php loop separate here but I have queries where I cannot use inner joins and a problem similar to this happens (which im about to explain).

The subquery for the where clause returns many email addresses, and I want to then bring up any log events relating to any of them. My problem is I then get an error message 'Subquery returns more than 1 row'.

If anyone could help that would be much appreciated!

0

3 Answers 3

3

I think this should work:

SELECT event FROM log
WHERE user IN
  (SELECT SUBSTRING(details,55) FROM log WHERE details LIKE 'ID: 308%')
Sign up to request clarification or add additional context in comments.

1 Comment

IN is the usual way to do it.
2

Use WHERE user IN <subquery> instead of WHERE user = <subquery>.

However, in my experience, MySQL's performance of IN <subquery> is usually very poor. This can always be rewritten as a JOIN, and that usually performs better.

Here's your example query rewritten as a JOIN:

SELECT event
FROM log l1
JOIN (SELECT DISTINCT SUBSTRING(details,55) loguser
      FROM log
      WHERE details LIKE 'ID: 308%') l2
ON l1.user = l2.loguser

In this particular case, I suspect performance will be similar. Where MySQL usually gets it wrong is when the subquery in the JOIN returns an indexed column, and you're joining with an indexed column in the main table. MySQL decides to use the index in the subquery instead of the main table, and that's usually wrong (in most cases of queries like this, the subquery returns a small subset of values).

1 Comment

How would you replace the IN with a JOIN ?
0

Your other option is to use EXISTS

SELECT `event`
FROM log
WHERE EXISTS(SELECT *
             FROM log AS log1
             WHERE log1.details LIKE 'ID: 308%'
               AND log.user = SUBSTRING(log1.details,55))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.