473,779 Members | 2,089 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 4697
On Tue, 20 May 2008 09:20:39 -0500, Mitch Sherman
<mi***********@ hush.aiwrote:
>On Mon, 19 May 2008 19:58:41 -0400, Jerry Stuckle
<js*******@att global.netwrote :
>>Mike Lahey wrote:
No, I NEVER SAID COUNT DISTINCT is a MySQL extension. Learn to read, idiot.

What you said was that an SQL query like this wasn't standard:

SELECT userid FROM test WHERE groupid IN (1, 2, 3, 4, 5)
GROUP BY userid HAVING COUNT(DISTINCT userid, groupid) = 5;

You went to great lengths to assert this "fact":
>>Mike Lahey wrote:
Yes, you would use count(DISTINCT entry_id, category_id) in the
HAVING clause.
Jerry Stuckle wrote:
Which is not standard SQL and will not work in all databases. It is a
MySQL extension, and probably will work only in that database. The
correct solution is portable across all databases.

Jerry Stuckle wrote:
>More show of ignorance. I never said COUNT DISTINCT wasn't defined in
ANSI SQL. I said the HAVING clause is NOT legal in ANSI SQL. But
you're too stoopid to understand why. And I'm NOT going to get into it
in a PHP newsgroup.

Totally wrong. This is completely valid SQL. It is not a "MySQL
extension."

You are trying real hard to spread some Jerrytastic FUD around.

If you still doubt, try it out here at the online SQL parser:

http://www.wangz.net/gsqlparser/sqlpp/sqlformat.htm
>>On May 15, 10:50 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
CoreyJansen wrote:
Jerry's approach results in a "cartesian explosion."
>
Then you have a broken database server. You need to report
that as a bug to MySQL ASAP. A lot of people depend self-
join queries like this!

Again, put up or shut up. Let's your evidence that this ia a bug. Oh
that's right, you haven't got any!

Crackpot.

F'ing idiot.

Oh of course, anyone who tries to help you and correct your error is
an "idiot!"

Mitch
I think I misrepresented what Jerry Stuckle posted here. Disregard my
post.

Mitch
Jun 27 '08 #91
Disregard all my posts from prior days in this thread please. It looks
like I misunderstood what Jerry, Guillaume, Tim Roberts, Captain
Paralytic, and others were saying and made a fool of myself!

I apologize for this to all.

Mitch

<mi***********@ hush.aiwrote:
>On Mon, 19 May 2008 19:58:41 -0400, Jerry Stuckle
<js*******@att global.netwrote :
>>Mike Lahey wrote:
No, I NEVER SAID COUNT DISTINCT is a MySQL extension. Learn to read, idiot.

What you said was that an SQL query like this wasn't standard:

SELECT userid FROM test WHERE groupid IN (1, 2, 3, 4, 5)
GROUP BY userid HAVING COUNT(DISTINCT userid, groupid) = 5;

You went to great lengths to assert this "fact":
>>Mike Lahey wrote:
Yes, you would use count(DISTINCT entry_id, category_id) in the
HAVING clause.
Jerry Stuckle wrote:
Which is not standard SQL and will not work in all databases. It is a
MySQL extension, and probably will work only in that database. The
correct solution is portable across all databases.

Jerry Stuckle wrote:
>More show of ignorance. I never said COUNT DISTINCT wasn't defined in
ANSI SQL. I said the HAVING clause is NOT legal in ANSI SQL. But
you're too stoopid to understand why. And I'm NOT going to get into it
in a PHP newsgroup.

Totally wrong. This is completely valid SQL. It is not a "MySQL
extension."

You are trying real hard to spread some Jerrytastic FUD around.

If you still doubt, try it out here at the online SQL parser:

http://www.wangz.net/gsqlparser/sqlpp/sqlformat.htm
>>On May 15, 10:50 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
CoreyJansen wrote:
Jerry's approach results in a "cartesian explosion."
>
Then you have a broken database server. You need to report
that as a bug to MySQL ASAP. A lot of people depend self-
join queries like this!

Again, put up or shut up. Let's your evidence that this ia a bug. Oh
that's right, you haven't got any!

Crackpot.

F'ing idiot.

Oh of course, anyone who tries to help you and correct your error is
an "idiot!"

Mitch
Jun 27 '08 #92
On Tue, 20 May 2008 09:20:39 -0500, Mitch Sherman
<mi***********@ hush.aiwrote:
>On Mon, 19 May 2008 19:58:41 -0400, Jerry Stuckle
<js*******@att global.netwrote :
>>Mike Lahey wrote:
No, I NEVER SAID COUNT DISTINCT is a MySQL extension. Learn to read, idiot.

What you said was that an SQL query like this wasn't standard:

SELECT userid FROM test WHERE groupid IN (1, 2, 3, 4, 5)
GROUP BY userid HAVING COUNT(DISTINCT userid, groupid) = 5;

You went to great lengths to assert this "fact":
>>Mike Lahey wrote:
Yes, you would use count(DISTINCT entry_id, category_id) in the
HAVING clause.
Jerry Stuckle wrote:
Which is not standard SQL and will not work in all databases. It is a
MySQL extension, and probably will work only in that database. The
correct solution is portable across all databases.

Jerry Stuckle wrote:
>More show of ignorance. I never said COUNT DISTINCT wasn't defined in
ANSI SQL. I said the HAVING clause is NOT legal in ANSI SQL. But
you're too stoopid to understand why. And I'm NOT going to get into it
in a PHP newsgroup.

Totally wrong. This is completely valid SQL. It is not a "MySQL
extension."

You are trying real hard to spread some Jerrytastic FUD around.

If you still doubt, try it out here at the online SQL parser:

http://www.wangz.net/gsqlparser/sqlpp/sqlformat.htm
>>On May 15, 10:50 am, Jerry Stuckle <jstuck...@attg lobal.netwrote:
CoreyJansen wrote:
Jerry's approach results in a "cartesian explosion."
>
Then you have a broken database server. You need to report
that as a bug to MySQL ASAP. A lot of people depend self-
join queries like this!

Again, put up or shut up. Let's your evidence that this ia a bug. Oh
that's right, you haven't got any!

Crackpot.

F'ing idiot.

Oh of course, anyone who tries to help you and correct your error is
an "idiot!"

Mitch
Jerry's a brilliant guy. Disregard this false and rude post please.

Mitch
Jun 27 '08 #93
On Thu, 22 May 2008 01:52:44 -0500, Mitch Sherman
<mi***********@ hush.aiwrote:
>On Thu, 22 May 2008 04:39:17 GMT, Tim Roberts <ti**@probo.com 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.

No, in SQL-92, aggregate functions like SUM and COUNT in the HAVING
clause may reference source columns, so this query is valid:

"Each <column referencecontai ned in a <subqueryin the <search
conditionthat references a column of T shall reference a
grouping column of T or shall be specified within a <set function
specification>. "
This quote I pasted is irrelevant... That's about subqueries. You have
to look elsewhere to find the other one...

Mitch
Jun 27 '08 #94
Disregard the foolish the posts I made before Jan 2 in this thread.
Looks like I didn't understand the replies made by others!

Mitch

On Mon, 02 Jun 2008 13:28:46 -0500, Mitch Sherman
<mi***********@ hush.aiwrote:
>On Tue, 13 May 2008 12:02:59 -0400, Jerry Stuckle
<js*******@att global.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 #95
On Thu, 15 May 2008 21:49:03 -0500, Mitch Sherman
<mi***********@ hush.aiwrote:
>
Your script doesn't test the same scenario at all. The table you
created is guaranteed not to have any duplicates because you defined a
PRIMARY KEY. This is exactly what you've been arguing against doing
all this time, so you've basically demonstrated why uniqueness is a
good thing.

Mitch
No I was wrong, Jerry's test shows another reasonable scenario which
the poster could be using and is a valid test of the original problem.
It's a counterexample to petersprc's original answer. petersprc's
answer needs to be modified as shown elsewhere in this thread to work
with Jerry's scenario.

Mitch

Jun 27 '08 #96
Mike Lahey wrote:
He said entries belong to categories, which is a fairly obvious
relationship. This is a binary relation. Thus there must exist a 2
column relational table in this design, or it's not normalized. Yes,
other tables may exist, but it's not relevant.
Yes well I was wrong in the post I'm replying to as well. Jerry's
scenario is totally consistent with the poster's scenario.
Jun 27 '08 #97
Mike Lahey wrote:
It's a red herring because
This post was very rude. Please disregard it.
Jun 27 '08 #98
Mike Lahey wrote:

This was a rude post and I was wrong. Please disregard it.
Jun 27 '08 #99
On May 12, 10:55 am, aguillacu...@gm ail.com wrote:
On May 12, 9:20 am, Captain Paralytic <paul_laut...@y ahoo.comwrote:
On 12 May, 02:36, Mike Lahey <mikey6...@yaho o.comwrote:
Pay attention to the posts. Peter's solution will work.
It may work, but that does not make it the "correct" way to do it.

Oh no, the answer is correct, but there are alternatives.
Well my post was not quite accurate. The answer wasn't correct it
imposed an unnecessary restriction - that the 2 columns couldn't have
duplicates. However, the answer is still useful if your data fits that
model and you want to intersect arbitrary #'s of groups....
Jun 27 '08 #100

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
1995
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
1805
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
3722
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
3016
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
1537
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
9474
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
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...
0
10139
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
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
9931
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6727
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
5373
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...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2869
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.