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
https://forums.phpfreaks.com/topic/3325-query-on-queries-multiple-in-one/
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]
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.