views:

62

answers:

1

I have a table with 2 columns: integer and var char. I am given only the integer values but need to do work on the var char (string) values.

Given an integer, and a list of other integers (no overlap), I want to find the string for that single integer. Then I want to take that string and do the INSTR command with that string, and all the other strings for all the other integers. Then I want the sum of all the INSTR so the result is one number.

So lets say I have int x, and list y=[y0, y1, y2].

I want to do 3 INSTR commands like

SUM(INSTR(string for x, string for y0), INSTR(string for x, string for y1), INSTR(string for x, string for y2))

I think im going in the wrong direction, this is what I have. Im not good with sub queries.

SELECT SUM
(
  SELECT INSTR
  (
    SELECT string FROM pages WHERE int=? LIMIT 1,
    (
      SELECT string FROM pages WHERE id=? OR id=? OR id=? LIMIT 3
    )
  )
)
A: 

I mostly use MS SQL Server, but you can try this in MySQL. You basically want to be using joins, not subqueries:

SELECT
    SUM(INSTR(x.string, y.string))
FROM
    pages x
INNER JOIN pages y ON
    y.int IN (?, ?, ?) AND
    INSTR(x.string, y.string) > 0
WHERE
    x.int = ?

Of course, "int" for a column name should be changed.

Tom H.