473,769 Members | 5,727 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 4692
I tried both queries, and the result is Jerry's method produces very
strange results. The normalized approach posted by petersprc does give
the expected result though.

For a table containing a a few thousand records with duplicates, Jerry's
query returned 200 million rows (yes 200 million) after running for
about 2 minutes. That's more rows than there were in the original table.

I copied the query directly into a test case.

DROP PROCEDURE IF EXISTS setup;

DELIMITER //

CREATE PROCEDURE setup ()
BEGIN
DECLARE i INT DEFAULT 0;
DROP TABLE IF EXISTS test;
CREATE TABLE test (entry_id int,
cat_id int);
WHILE i < 10000 DO
INSERT INTO test VALUES (2, 30),
(2, 35), (3, 30), (3, 35);
SET i = i + 1;
END WHILE;
END;

//

DELIMITER ;

CALL setup();

DROP TABLE IF EXISTS result;

CREATE TABLE result AS
SELECT a.entry_id
FROM test a
INNER JOIN test b
ON a.entry_id = b.entry_id
WHERE a.cat_id = 30
AND b.cat_id = 35;

The output is:

Query OK, 200000000 rows affected (2 min 6.35 sec)
Records: 200000000 Duplicates: 0 Warnings: 0

Jerry's approach results in a "cartesian explosion."

--
Corey Jansen
cc*****@gmail.c om
Jun 2 '08 #21
On May 15, 12:27 am, Corey Jansen <ccj9...@gmail. comwrote:
Jerry's approach results in a "cartesian explosion."
Which is exactly the problem database normalization is designed to
prevent.

If only Mr. Stuckle had listened to what 10 people told him already.
Obstinacy is his best policy it seems :)

Yet another lesson in "Why You Should Use Proper Database Design."
Jun 2 '08 #22
Corey Jansen wrote:
I tried both queries, and the result is Jerry's method produces very
strange results. The normalized approach posted by petersprc does give
the expected result though.

For a table containing a a few thousand records with duplicates, Jerry's
query returned 200 million rows (yes 200 million) after running for
about 2 minutes. That's more rows than there were in the original table.
Those that can, do. Those that can't, teach. ;-)

If I had a tenner for every 'theoretically correct' approach that has
resulted in hgue software size, or machine overhead, or just plain not
working..
I copied the query directly into a test case.

DROP PROCEDURE IF EXISTS setup;

DELIMITER //

CREATE PROCEDURE setup ()
BEGIN
DECLARE i INT DEFAULT 0;
DROP TABLE IF EXISTS test;
CREATE TABLE test (entry_id int,
cat_id int);
WHILE i < 10000 DO
INSERT INTO test VALUES (2, 30),
(2, 35), (3, 30), (3, 35);
SET i = i + 1;
END WHILE;
END;

//

DELIMITER ;

CALL setup();

DROP TABLE IF EXISTS result;

CREATE TABLE result AS
SELECT a.entry_id
FROM test a
INNER JOIN test b
ON a.entry_id = b.entry_id
WHERE a.cat_id = 30
AND b.cat_id = 35;

The output is:

Query OK, 200000000 rows affected (2 min 6.35 sec)
Records: 200000000 Duplicates: 0 Warnings: 0

Jerry's approach results in a "cartesian explosion."
I'll remember that phrase...
>
Jun 2 '08 #23
Corey Jansen wrote:
I tried both queries, and the result is Jerry's method produces very
strange results. The normalized approach posted by petersprc does give
the expected result though.

For a table containing a a few thousand records with duplicates, Jerry's
query returned 200 million rows (yes 200 million) after running for
about 2 minutes. That's more rows than there were in the original table.

I copied the query directly into a test case.

DROP PROCEDURE IF EXISTS setup;

DELIMITER //

CREATE PROCEDURE setup ()
BEGIN
DECLARE i INT DEFAULT 0;
DROP TABLE IF EXISTS test;
CREATE TABLE test (entry_id int,
cat_id int);
WHILE i < 10000 DO
INSERT INTO test VALUES (2, 30),
(2, 35), (3, 30), (3, 35);
SET i = i + 1;
END WHILE;
END;

//

DELIMITER ;

CALL setup();

DROP TABLE IF EXISTS result;

CREATE TABLE result AS
SELECT a.entry_id
FROM test a
INNER JOIN test b
ON a.entry_id = b.entry_id
WHERE a.cat_id = 30
AND b.cat_id = 35;

The output is:

Query OK, 200000000 rows affected (2 min 6.35 sec)
Records: 200000000 Duplicates: 0 Warnings: 0

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!

This works fine (sorry about the line wraps):

<?php

$link = mysql_connect(' localhost', 'root', 'vps11131') or die("Can't
connect: " . mysql_error());
$db = mysql_select_db ('test');

// Clear table if it existed
mysql_query('DR OP TABLE IF EXISTS test');
mysql_query('CR EATE TABLE test (groupid INT NOT NULL, ' .
'userid INT NOT NULL, PRIMARY KEY(groupid, userid))');

// Insert 10K rows of data
for ($i = 1; $i <= 100; $i++)
for ($j = 1; $j<= 100; $j++)
mysql_query("IN SERT INTO test(groupid, userid) VALUES($i, $j)");

// Now lets get rid of some of the data so we have meaningful results
mysql_query('DE LETE FROM test WHERE groupid=32 AND MOD(userid, 3) 0');
mysql_query('DE LETE FROM test WHERE groupid=38 AND MOD(userid, 4) 0');

// Pull the matching data from the table

$result = mysql_query('SE LECT a.userid AS userid ' .
'FROM test a ' .
'INNER JOIN test b ' .
'ON a.userid = b.userid ' .
'WHERE a.groupid = 32 ' .
'AND b.groupid = 35');
echo 'Rows found: ' . mysql_num_rows( $result) . "\n";
while ($data = mysql_fetch_arr ay($result))
echo $data['userid'] . " ";
mysql_close();
?>

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #24
vk*****@gmail.c om wrote:
On May 15, 12:27 am, Corey Jansen <ccj9...@gmail. comwrote:
>Jerry's approach results in a "cartesian explosion."

Which is exactly the problem database normalization is designed to
prevent.

If only Mr. Stuckle had listened to what 10 people told him already.
Obstinacy is his best policy it seems :)

Yet another lesson in "Why You Should Use Proper Database Design."
I'm not arguing about proper database design. My only comment is it is
IMPOSSIBLE to determine if the database is normalized or not from the
given information. There could be one or more additional columns to
determine uniqueness, for instance.

And people wonder why I send folks to comp.databases. mysql for MySQL
questions - that's where the REAL experts hang out.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #25
The Natural Philosopher wrote:
Corey Jansen wrote:
>I tried both queries, and the result is Jerry's method produces very
strange results. The normalized approach posted by petersprc does give
the expected result though.

For a table containing a a few thousand records with duplicates,
Jerry's query returned 200 million rows (yes 200 million) after
running for about 2 minutes. That's more rows than there were in the
original table.

Those that can, do. Those that can't, teach. ;-)
And those who can't teach become philosophers.
If I had a tenner for every 'theoretically correct' approach that has
resulted in hgue software size, or machine overhead, or just plain not
working..

If I had a tenner for every good comment you made, I'd be broke.
However, if I had ten cents for every stupid remark you made, I could
retire.
>
>I copied the query directly into a test case.

DROP PROCEDURE IF EXISTS setup;

DELIMITER //

CREATE PROCEDURE setup ()
BEGIN
DECLARE i INT DEFAULT 0;
DROP TABLE IF EXISTS test;
CREATE TABLE test (entry_id int,
cat_id int);
WHILE i < 10000 DO
INSERT INTO test VALUES (2, 30),
(2, 35), (3, 30), (3, 35);
SET i = i + 1;
END WHILE;
END;

//

DELIMITER ;

CALL setup();

DROP TABLE IF EXISTS result;

CREATE TABLE result AS
SELECT a.entry_id
FROM test a
INNER JOIN test b
ON a.entry_id = b.entry_id
WHERE a.cat_id = 30
AND b.cat_id = 35;

The output is:

Query OK, 200000000 rows affected (2 min 6.35 sec)
Records: 200000000 Duplicates: 0 Warnings: 0

Jerry's approach results in a "cartesian explosion."

I'll remember that phrase...
>>
ROFLMAO. Never heard of a cartesian product?

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #26
vk*****@gmail.c om wrote:
On May 14, 7:21 pm, Mike Lahey <mikey6...@yaho o.comwrote:
>Jerry Stuckle wrote:
>>No argument.
But that was an additional condition the poster required - not the
original op. And that's what makes it incorrect.
Uniqueness is a consequence of the relationship the OP wanted to model.
Best practice is to create an index, which is the correct solution, as
has been pointed out several times.

You should properly normalize your DB instead of working around a broken
design as you're arguing for.

Amen. Any proposed solution that skips this step is incomplete. One
shouldn't rely on a broken data model and expect to get good results.
No arguments. But based on the information given, we cannot say the
database was not normalized.
>The OP wanted to indicate membership in a group. A membership relation
does not contain duplicates.

Yes, by definition, a membership set has no dups. To take another
example, it wouldn't be proper for a student to belong to the same
class twice. (He could repeat the course, but that wouldn't be the
same class would it.)
It depends. For instance, you could have an additional column -
privileges. Things like "read", "post", "upload" to determine the
rights the user has.
Using a flawed db design creates all sorts of inconsistencies which
are better to avoid when developing robust systems.

Jerry's suggested query blows up when faced with duplicates, so you
can see how easy it is to fall into this trap.
My query does not blow up with there are duplicates. It works perfectly
well. But Peter's fails in that case.

And people wonder why I refer MySQL questions to comp.databases. mysql -
where the real experts hang out.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #27
On Thu, 15 May 2008 11:55:29 -0400, Jerry Stuckle
<js*******@attg lobal.netwrote:
>vk*****@gmail. com wrote:
>On May 15, 12:27 am, Corey Jansen <ccj9...@gmail. comwrote:
>>Jerry's approach results in a "cartesian explosion."

Which is exactly the problem database normalization is designed to
prevent.

If only Mr. Stuckle had listened to what 10 people told him already.
Obstinacy is his best policy it seems :)

Yet another lesson in "Why You Should Use Proper Database Design."

I'm not arguing about proper database design. My only comment is it is
IMPOSSIBLE to determine if the database is normalized or not from the
given information.
That doesn't mean that the relation can't be normalized first. That
seems to be the critical point you're missing.

You seem to arguing that it's better to build on a potentially flawed
database design rather than get it right first, which is terrible
advice.
There could be one or more additional columns to determine uniqueness, for instance.

And people wonder why I send folks to comp.databases. mysql for MySQL
questions - that's where the REAL experts hang out.
This is a pointless hypothetical. If you have N columns, you can still
maintain uniqueness across those columns. That doesn't require
duplicate rows any more than the original problem which had only 2
columns.

Mitch
Jun 2 '08 #28
On Thu, 15 May 2008 11:50:57 -0400, Jerry Stuckle
<js*******@attg lobal.netwrote:
>Corey Jansen 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!
Not at all, this is a bug in your query. It produced the same result
here. MySQL did exactly what you told it to do. You seem desperate to
avoid acknowledging this, resorting even to making up fictitious MySQL
bug reports.

The problem is you are self-joining using a condition that isn't
unique and lacks a primary key reference. Sometimes this is what you
want, but that is not the case in the original problem.

Let me spell it out for you. Let's say you have rows A through F that
contain the following values:

A: (2, 30)
B: (2, 35)
C: (2, 30)
D: (2, 35)
E: (2, 30)
F: (2, 35)

There are only 6 rows in the table. Your query, however, will produce
more than 6 matches. This is because rows A, C, and E can each be
paired a total of 3 times. The result of the inner join is:

(A, B), (A, D), (A, F)
(C, B), (C, D), (C, F)
(E, B), (E, D), (E, F)

Now, here's how it looks in SQL:

-- Create the table with 6 rows --

DROP TABLE IF EXISTS test;
CREATE TABLE test (entry_id int, cat_id int);
INSERT INTO test (entry_id, cat_id) values
(2, 30), (2, 35), (2, 30), (2, 35), (2, 30),
(2, 35);

-- Run the query --

SELECT a.entry_id FROM test a INNER JOIN test b
ON a.entry_id = b.entry_id WHERE a.entry_id =
b.entry_id AND a.cat_id = 30 AND b.cat_id = 35;

The result of your query is:

9 rows in set (0.00 sec)

This gets worse as your table gets bigger. You end up with the
"cartesian explosion" in the test case that you are denying exists.
>
This works fine (sorry about the line wraps):

<?php

$link = mysql_connect(' localhost', 'root', 'vps11131') or die("Can't
connect: " . mysql_error());
$db = mysql_select_db ('test');

// Clear table if it existed
mysql_query('D ROP TABLE IF EXISTS test');
mysql_query('C REATE TABLE test (groupid INT NOT NULL, ' .
'userid INT NOT NULL, PRIMARY KEY(groupid, userid))');
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
Jun 2 '08 #29
Mitch Sherman wrote:
On Thu, 15 May 2008 11:55:29 -0400, Jerry Stuckle
<js*******@attg lobal.netwrote:
>vk*****@gmail.c om wrote:
>>On May 15, 12:27 am, Corey Jansen <ccj9...@gmail. comwrote:
Jerry's approach results in a "cartesian explosion."
Which is exactly the problem database normalization is designed to
prevent.

If only Mr. Stuckle had listened to what 10 people told him already.
Obstinacy is his best policy it seems :)

Yet another lesson in "Why You Should Use Proper Database Design."
I'm not arguing about proper database design. My only comment is it is
IMPOSSIBLE to determine if the database is normalized or not from the
given information.

That doesn't mean that the relation can't be normalized first. That
seems to be the critical point you're missing.
No, the critical point YOU'RE MISSING is that the table may be
normalized - AND STILL HAVE DUPLICATES IN THESE COLUMNS.

That is the critical point!
You seem to arguing that it's better to build on a potentially flawed
database design rather than get it right first, which is terrible
advice.
No, I'm not. There is nothing flawed about a design which has three
columns (of which these are only two) determining the primary key (or
other unique value).
>There could be one or more additional columns to determine uniqueness, for instance.

And people wonder why I send folks to comp.databases. mysql for MySQL
questions - that's where the REAL experts hang out.

This is a pointless hypothetical. If you have N columns, you can still
maintain uniqueness across those columns. That doesn't require
duplicate rows any more than the original problem which had only 2
columns.

Mitch
No, it is not pointlessly hypothetical. It is very germane to this
situation. We do not have all of the information - the complete
database design, usage, etc.

The other column(s) may not be germane to the problem, so the original
op did not list them. That is quite common - and correct - as it does
not confuse the issue at hand with irrelevant data. There may very well
have been 2 columns - or 20 columns or even 200 columns. You don't know
which is correct.

For instance, here's a table which could very well be the case:

userid groupid permission
1 1 read
1 1 write
1 1 delete
1 2 read
1 3 read

This is a commonly used design. The permission column is not pertinent
to the original ops question - so it wouldn't be listed. But Peter's
query will fail if it looks for someone who is a member if groups 1 and
2. The correct query works in this case just fine.

My God, I've never seen someone so insistent about making false
assumptions about someone else's code - and so stubborn about sticking
to a bad suggestion.

I really suggest you learn some more advanced sql - actually, the
correct answer isn't even advanced level. I'm not sure it even makes
intermediate level.

The correct query works 100% of the time - whether there are duplicates
or not.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===

Jun 2 '08 #30

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

Similar topics

4
9798
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
1994
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
1536
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
1813
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
3720
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
5575
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
3014
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
1536
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
9423
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
10039
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...
0
9860
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
8869
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
6668
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
5297
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
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3560
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
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.