473,545 Members | 582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subquery problems

I have a query :

=============== ==========
SELECT users.username, posts.post_id, users.user_id, posts.post_time ,
post_text.post_ subject, post_text.post_ text, topics.topic_ti tle
FROM `phpbbtes_posts ` posts, `phpbbtes_posts _text` post_text,
`phpbbtes_users ` users, `phpbbtes_topic s` topics
WHERE posts.post_id = post_text.post_ id
AND posts.poster_id = users.user_id
AND posts.topic_id = topics.topic_id
AND topics.forum_id NOT IN (
SELECT forum_id
FROM phpbbtes_hidden _forums
)
ORDER BY posts.post_time DESC
LIMIT 0 , 5
=============== ============

When I hardcode the values in the subquery like this :

=============== ============
SELECT users.username, posts.post_id, users.user_id, posts.post_time ,
post_text.post_ subject, post_text.post_ text, topics.topic_ti tle
FROM `phpbbtes_posts ` posts, `phpbbtes_posts _text` post_text,
`phpbbtes_users ` users, `phpbbtes_topic s` topics
WHERE posts.post_id = post_text.post_ id
AND posts.poster_id = users.user_id
AND posts.topic_id = topics.topic_id
AND topics.forum_id NOT
IN (
84,12,34,116,38 ,54,39,43,48,90 ,104,105,107,10 8,113,110,111,1 12,60,94,64,85, 91,117,127,128, 130,126,123,129 ,124,131,132,12 5,119,118,121,1 20,122,28,8,106 ,7,92,9,26,86,1 15,93
)
ORDER BY posts.post_time DESC
LIMIT 0 , 5
=============== =============

everything runs okay.

The topics.forum_id is smallint(8) and the forum_id is smallint(8)

I keep getting an error 1064.

Any help would be most appreciated.

Chris
Jul 20 '05 #1
2 1473
Chris wrote:
I have a query :

=============== ==========
SELECT users.username, posts.post_id, users.user_id, posts.post_time ,
post_text.post_ subject, post_text.post_ text, topics.topic_ti tle
FROM `phpbbtes_posts ` posts, `phpbbtes_posts _text` post_text,
`phpbbtes_users ` users, `phpbbtes_topic s` topics
WHERE posts.post_id = post_text.post_ id
AND posts.poster_id = users.user_id
AND posts.topic_id = topics.topic_id
AND topics.forum_id NOT IN (
SELECT forum_id
FROM phpbbtes_hidden _forums
)
ORDER BY posts.post_time DESC
LIMIT 0 , 5
=============== ============

When I hardcode the values in the subquery like this :

=============== ============
SELECT users.username, posts.post_id, users.user_id, posts.post_time ,
post_text.post_ subject, post_text.post_ text, topics.topic_ti tle
FROM `phpbbtes_posts ` posts, `phpbbtes_posts _text` post_text,
`phpbbtes_users ` users, `phpbbtes_topic s` topics
WHERE posts.post_id = post_text.post_ id
AND posts.poster_id = users.user_id
AND posts.topic_id = topics.topic_id
AND topics.forum_id NOT
IN (
84,12,34,116,38 ,54,39,43,48,90 ,104,105,107,10 8,113,110,111,1 12,60,94,64,85, 91,117,127,128, 130,126,123,129 ,124,131,132,12 5,119,118,121,1 20,122,28,8,106 ,7,92,9,26,86,1 15,93 )
ORDER BY posts.post_time DESC
LIMIT 0 , 5
=============== =============

everything runs okay.

The topics.forum_id is smallint(8) and the forum_id is smallint(8)

I keep getting an error 1064.

Any help would be most appreciated.


Subqueries are not supported in MySQL until version 4.1. I'm assuming you're
using 4.0.x or lower for you to be getting this error. 4.1 has only just
been released into production so it will be a while yet before any web
hosts start putting it on production servers.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 20 '05 #2
In general, when you use a subquery for a NOT IN predicate, you can
achieve the same result with a LEFT OUTER JOIN and test for the right
hand side of that join being NULL (be sure to choose a field from the
right hand side table that is NOT NULL otherwise).

So, for your example (stripped down):

SELECT ...
FROM topics t LEFT OUTER JOIN phpbbtes_hidden _forums p
ON (t.forum_id = p.forum_id)
WHERE p.forum_id IS NULL

Regards,
Bill K.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4565
by: lev | last post by:
CREATE TABLE . ( NULL , , (44) ) ID is non-unique. I want to select all IDs where the last entry for that ID is of type 11.
7
9190
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into a table (c) re-queries another table using a subquery which references the inserted table (correlated or not)
0
563
by: Greg Stark | last post by:
Postgresql 7.4b2 (approximately, compiled out of CVS) When I have a subquery that has a complex subquery as one of the result columns, and then that result column is used multiple times in the parent query, the subquery is inlined for each one. This means multiple redundant executions of the subquery. I recall there was a way to defeat...
2
5368
by: edself | last post by:
Greetings, I am semi-new to Access and have a query question. I presume the solution is easy, but need some help. I have created a database with a Contact table. The contact table contains address fields among other things. Because some contacts share the same address, I included a boolean field, PrimaryContact. If true, a given...
8
19575
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two...
7
2359
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller...
2
1463
by: dcourington | last post by:
I'm new to this group and not an experienced Access user, but have solved several of my problems already by reading other threads on this forum (Thanks!). Here's my current problem that I can't figure out how to solve: I'm trying to calculate total project management budget by project phase. I have a query that returns data grouped by project...
5
8015
by: Anne | last post by:
Hello! Here is the statement in question: --STATEMENT A SELECT * FROM dbo.myTable WHERE colX in (SELECT colX FROM dbo.sourceTable) The problem with Statement A is that 'colX' does not exist in 'dbo.sourceTable'. It does, however, certainly exist in 'dbo.myTable'. Breaking the statement down, we have:
1
4137
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only allows me to filter based on variables in a single table. I would like to have a search form where I can select multiple variables (from various...
0
7391
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7746
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5962
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4941
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
693
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.