Jump to content

Query on Queries. Multiple in one


Humpty

Recommended Posts

G'day guys,
I'm a newbie for PHP, and I have done some SQL for a while but I am lost when it gets too advanced. Need more practice.

What i need is a query that gives results based on criteria that is a query itself (i think)

This should explain it:

What I want:
SELECT * FROM table1 WHERE field2 [i]isn't listed in table2 field7[/i]

I hope that made logical sense to someone. :D

(love heart added as symbol of worldy love for all)
Link to comment
Share on other sites

Subqueries are almost always slower than joins. If you can figure out a join to use instead, you should.

This kind of query is a common question among newcomers to SQL, here is the solution:

[code]
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.field2=t2.field7 WHERE t2.field7 IS NULL
[/code]
Link to comment
Share on other sites

First, there are many cases where you simply can't use a JOIN. Second, the main reason why subqueries are often slower is simply that MySQL can't figure out a good way to optimize it -- and this should get better with time. Still, JOINs are "better" from a speed perspective, but "harder" for some people to visualize.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.