473,791 Members | 3,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help writing SQL statement in PHP script

This might be in the wrong group, but...

Here is an example of my data:

entry_id cat_id
1 20
2 25
3 30
4 25
5 35
6 25
2 30
2 35
3 35

As you can see, entry_id's 2 and 3 both belong to cat_id 30 and 35

I have captured the cat_id's 30 and 35 with my script, so I need all
entry_id's that belong to BOTH cat_id 30 and 35.

I tried "Select entry_id from myTable where cat_id = '30' and cat_id =
'35' but obviously that is incorrect.

Can someone help? Thanks...
Jun 2 '08
118 4701
On May 22, 7:36 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
Tim Roberts wrote:
No, in this case, Jerry is right and you are wrong. In strict ANSI SQL,
any column reference in the HAVING clause must either be part of the result
set or one of the grouped columns. "groupid" is not present in the result
set, nor is it grouped.

Tim,

Don't bother arguing with the idiot. He'll never admit that he's wrong.

He's even quoting a standard that has been out of date for over 10 years!
This query is valid in every version since SQL-92.

The aggregate function aren't restricted to referencing
only grouping columns.

This part defines the relevant restriction on column
references in the HAVING clause:

Subclause 7.10, Syntax Rules, #3: "Each column
reference directly contained in the search condition
shall be one of the following: a) An unambiguous
reference to a column that is functionally dependent
on G. b) An outer reference."
[SQL/Foundation 2003/08]

This rule doesn't apply to columns in aggregate
functions because they aren't directly contained,
i.e.:

Subclause 6.3.3.1: "A1 directly contains B1 if A1
contains B1 without an intervening subquery, multiset
value constructor by query, table value constructor
by query, array value constructor by query, within
group specification, or set function specification
that is not an ordered set function."
[SQL/Framework 2003/08]

SQL-92 has a similar definition. Mitch Sherman was
incorrect in quoting the part about subqueries because
there is no subquery being used, but the query is still
valid for the reason he implied.

Aggregate functions like COUNT are set function
specifications and hence don't fall under rule #3
because the column references are not directly
contained in the HAVING condition. These aggregate
functions can reference any column in the argument
source, i.e.:

Subclause 6.7: "If QCR [query column reference] is
a within-group-varying column reference, then QCR
denotes the values of C in the rows of a given group
of the qualifying query of QCR used to construct the
argument source of a set function specification."
[SQL/Foundation 2003/08]

The argument source is a table or a group containing
N rows.

One example using this would be finding the customers
who transacted more than a certain amount in sales:

SELECT cust_id
FROM sale
GROUP by cust_id
HAVING SUM(amount) 500

There are some more examples of this pattern here:

http://www.cs.jcu.edu.au/Subjects/cp...html#section48
Jun 2 '08 #71
On Tue, 13 May 2008 12:02:59 -0400, Jerry Stuckle
<js*******@attg lobal.netwrote:
>>Not necessarily. What happens if he has two entries with (2,30)? It
will fail.
As I said - there was nothing in the original problem description to
prohibit it. And the correct answer doesn't require unique entries.
Sorry Jerry, I see that you're correct in this thred. I didn't mean to
suggest otherwise. The correct answer should work with duplicates or
without. Peter's answer is designed to work in a limited scenario.
Your answer (with DISTINCT as you said) is a better response.

Thanks for your help in understanding this SQL issue. I can see you
have a lot of experience in this area which helps to understand how to
properly model these problems.

Thanks again for your expert SQL recommendations in this thread and
many others.

Mitch
Jun 27 '08 #72
Mike Lahey wrote:
Jerry Stuckle wrote:
>You'll get good answers there because that's where the SQL experts
hang out. The answers posted here so far are incorrect.

Pay attention to the posts. Peter's solution will work.

You can learn about grouping queries at this tutorial page:
http://www.w3schools.com/sql/sql_groupby.asp
I was wrong too, and I apologize to Jerry Stuckle and the other posters
in this thread for being so obstinate and rude. I agree with Jerry's
analysis in this thread and he is correct as usual. Peter's solution
does not work in all cases, whereas Jerry's will. I retract my previous
posts in this thread which were rash and inappropriate.

Jerry was obviously correct all along. Thank you to Mr. Stuckle for his
brilliant analysis and patient explanations regarding this issue. I wont
waste any more of this group's time and will leave now.
Jun 27 '08 #73
Mike Lahey wrote:
>
>>
Not necessarily. What happens if he has two entries with (2,30)? It
will fail.

Nothing in the description of the problem prohibits such an occurrence.

That was addressed in the response, but it looks like you missed it.

You make the mistake of assuming the data is not normalized. I think the
point of Peter's post was to ensure that the data was indeed normalized.
I was wrong in this post, and I apologize to Jerry Stuckle and the other
posters in this thread for being so obstinate and rude. I agree with
Jerry's analysis in this thread and he is correct as usual. Peter's
solution does not work in all cases, whereas Jerry's will. I retract my
previous posts in this thread which were rash and inappropriate.

Jerry was obviously correct all along. Thank you to Mr. Stuckle for his
brilliant analysis and patient explanations regarding this issue. I wont
waste any more of this group's time and will leave now.

Jun 27 '08 #74
Mike Lahey wrote:
Jerry Stuckle wrote:
>And why is that nonsense? He told us about TWO COLUMNS in his table.
He never said these were THE ONLY TWO COLUMN. He might have had 200
more, for all we know. But being immaterial to the question at hand,
he would not have posted the superfluous information - which is the
correct thing to do.

False. His problem described a binary relation. Your mysterious
"200-column table", while it may exist, isn't relevant to the poster's
question.
[...]

I was wrong in this post, and I apologize to Jerry Stuckle and the other
posters in this thread for being so obstinate and rude. I agree with
Jerry's analysis in this thread and he is correct as usual. Peter's
solution does not work in all cases, whereas Jerry's will. I retract my
previous posts in this thread which were rash and inappropriate.

Jerry was obviously correct all along. Thank you to Mr. Stuckle for his
brilliant analysis and patient explanations regarding this issue. I wont
waste any more of this group's time and will leave now.
Jun 27 '08 #75
Mike Lahey wrote:
Jerry Stuckle wrote:
>Mitch Sherman wrote:
>No, my story has been consistent from the start. It's just some
stoopid morons here who can't understand that the table may be
normalized - but still have duplicates IN THESE TWO COLUMNS.

Who cares? If you are modeling a ternary relationship, then use COUNT
DISTINCT. This still has no bearing on the original problem however.
[...]

I was wrong in this post, and I apologize to Jerry Stuckle and the other
posters in this thread for being so obstinate and rude. I agree with
Jerry's analysis in this thread and he is correct as usual. Peter's
solution does not work in all cases, whereas Jerry's will. I retract my
previous posts in this thread which were rash and inappropriate.

Jerry was obviously correct all along. Thank you to Mr. Stuckle for his
brilliant analysis and patient explanations regarding this issue. I wont
waste any more of this group's time and will leave now.
Jun 27 '08 #76
Mike Lahey wrote:
Put up
[...]

I was wrong in this post, and I apologize to Jerry Stuckle and the other
posters in this thread for being so obstinate and rude. I agree with
Jerry's analysis in this thread and he is correct as usual. Peter's
solution does not work in all cases, whereas Jerry's will. I retract my
previous posts in this thread which were rash and inappropriate.

Jerry was obviously correct all along. Thank you to Mr. Stuckle for his
brilliant analysis and patient explanations regarding this issue. I wont
waste any more of this group's time and will leave now.
Jun 27 '08 #77
Mike Lahey wrote:
again, put up
[...]

I was wrong in this post, and I apologize to Jerry Stuckle and the other
posters in this thread for being so obstinate and rude. I agree with
Jerry's analysis in this thread and he is correct as usual. Peter's
solution does not work in all cases, whereas Jerry's will. I retract my
previous posts in this thread which were rash and inappropriate.

Jerry was obviously correct all along. Thank you to Mr. Stuckle for his
brilliant analysis and patient explanations regarding this issue. I wont
waste any more of this group's time and will leave now.
Jun 27 '08 #78
Jerry Stuckle wrote:
Chuck Cheeze wrote:

This isn't a PHP question - it's a database question. You need a group
for your database (i.e. if it's MySQL, comp.databases. mysql).

You'll get good answers there because that's where the SQL experts hang
out. The answers posted here so far are incorrect.
You were right Jerry, I'm sorry for starting a pointless argument with you.
Jun 27 '08 #79
Chuck Cheeze wrote:
This might be in the wrong group, but...

Here is an example of my data:

entry_id cat_id
1 20
2 25
3 30
4 25
5 35
6 25
2 30
2 35
3 35

As you can see, entry_id's 2 and 3 both belong to cat_id 30 and 35

I have captured the cat_id's 30 and 35 with my script, so I need all
entry_id's that belong to BOTH cat_id 30 and 35.

I tried "Select entry_id from myTable where cat_id = '30' and cat_id =
'35' but obviously that is incorrect.

Can someone help? Thanks...
I was wrong and I apologize to Jerry Stuckle and the other posters in
this thread for being so obstinate and rude. I agree with Jerry's
analysis in this thread and he is correct as usual. Peter's solution
does not work in all cases, whereas Jerry's will. I retract my previous
posts in this thread which were rash and inappropriate.

Jerry was obviously correct all along. Thank you to Mr. Stuckle for his
brilliant analysis and patient explanations regarding this issue. I wont
waste any more of this group's time and will leave now.
Jun 27 '08 #80

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

Similar topics

4
9799
by: Chuck100 | last post by:
I'm having problems with the output of the following script (I've simplified it):- select a.section,a.user,count(b.number),null from table a, table b where......... group by a.section,a.user union select a.section,a.user,null,count(c.number) from table a, table c
12
1996
by: Franklin P Patchey | last post by:
I have modified some script and think i have put a bit in that isn't "compliant" Is the bit marked below (towards the end) correct - should it be () and not ("") <SCRIPT LANGUAGE="JavaScript"> <!-- hiding page=new Date(); if (page.getDate() == 1) document.write("<embed src='media/audio/waltzinblack.mp3' width='145'
2
1537
by: ASallade | last post by:
Hello, I've scoured my books and the web, but am still daunted, hopefully some of the users in this newsgroup will have advice for my problem. I am not an experienced javascript programmer, but have gotten my code to work well, without errors on IE, Opera and netscapes recent builds. While testing, I found that it doesnt execute on Netscape 4.7 In the head I have a script that creates a custom object/class for products,
3
1814
by: Funnyweb | last post by:
When adding a field to a table using ALTER TABLE is it possible to check if the field already exits before the ADD command is run? If so how do I do this? Thanks Hamilton
2
1806
by: JPL Verhey | last post by:
(i hope somebody (else) will read and have an idea! Thnx) Hi, With a script in a popup window, I want to check if certain content is present in a page loaded into the frame "main" of the frameset in the opener window. I started with something like this (what happens when the considions are met already works):
1
3724
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am attach this script files and inq files. I cant understand this error. Please suggest me. You can talk with my yahoo id b_sahoo1@yahoo.com. Now i am online. Plz....Plz..Plz...
0
5576
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
12
3018
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed the instructions and couldnt get the correct results. heres the code stuff... temperature=input("what is the temperature of the spam?") if temperature>50: print "the salad is properly cooked." else:
3
1540
by: koutoo | last post by:
I have a code that writes to 2 seperate files. I keep getting a "list index out of range" error. The strange part is that when checking the files that I'm writing too, the script has already iterated through and finished writing, yet the error stated implies that it hasn't? So how can it be, that my script has written to the files, yet the error is stating that it hasn't made it through the script? I'll have 15 files that I have...
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10419
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9023
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6770
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5424
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4100
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3709
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2910
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.