SQL: Is the query the same as OK, or is there a more efficient way to do this, for example, using a join?

I often have to write an SQL query as follows:

SELECT body 
  FROM node_revisions 
 where vid = (SELECT vid 
                FROM node 
               WHERE nid = 4);

I know there are associations and things you could do, but they seem to complicate things. Is there a better way to do this? Is it more efficient? Easier to understand?

+5
source share
8 answers

Matches are generally more effective because the databases are written with the given operations in mind (and the union operations are performed).

However, performance will vary from database to database, how tables are structured, how much data there is, and how much will be returned by the query.

, , .

:

SELECT body 
FROM node_revisions nr
INNER JOIN node n
  ON nr.vid = n.vid
WHERE n.nid = 4

, , , node nid = 4, .

:

SELECT body 
FROM node_revisions 
WHERE vid IN (SELECT vid 
             FROM node 
             WHERE nid = 4);

? .

+7

, , , OP. ... ( , )

  • - (fishy), .
  • , .
  • , .
  • .
  • , .

*

, , . , , . . , , .

(* , , ).

select
  body

from node_revisions nr

join node n
on n.vid = nr.vid

where n.nid = 4
+3

, . , , , . :

SELECT body 
  FROM node_revisions 
    inner join node 
      on (node_revisions.vid = node.vid)
  WHERE node.nid = 4
+3

:

select body 
from node_revisions nr
join node n on nr.vid = n.vid
where n.vid = 4

[!]:

select body 
from node_revisions nr, node n
where n.nid = 4 and nr.vid = n.vid

, SQL Server , , " " , , !

+2
select 
     body 
from node_revisions A 
where exists (select 'x' 
              from Node B 
              Where A.Vid = B.Vid and B.NID=4)
+1

MySQL 6.x IN- INNER JOIN, , , 2 :

http://forge.mysql.com/worklog/task.php?id=3740

, , INNER JOIN - , , ( - ). , :

select body from node_revisions r, node n where r.vid = n.vid and n.node = 4
+1

, , , .

+1
SELECT  body 
FROM    node_revisions 
WHERE   vid =
        (
        SELECT  vid 
        FROM    node 
        WHERE   nid = 4
        )

, nid PRIMARY KEY UNIQUE.

: , , node 1 nid = 4.

nid PRIMARY KEY, JOIN .

node

const .

+1

All Articles