473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How can I do this without subqueries?

I have a table with this structure
CREATE TABLE `occurrence` (
`occurrence_id` int(10) unsigned NOT NULL auto_increment,
`word_id` int(10) unsigned NOT NULL default '0',
`productId` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`occurrence_id`)
);

The following query will show me how many times 'something' occurred
for each productId

SELECT productId, count(*) as occurrences
FROM occurrence AS o, word AS w
WHERE o.word_id = w.word_id and word_word = 'something' group by
productId order by occurrences

I want to be able to find out how many times multiple words occur for
each productId.
I want to be able to do something like this.

SELECT productId, count(*) as occurrences
FROM occurrence AS o, word AS w
WHERE o.word_id = w.word_id and word_word = 'something1' order by
occurrences
union
SELECT productId, count(*) as occurrences
FROM occurrence AS o, word AS w
WHERE o.word_id = w.word_id and word_word = 'something2' order by
occurrences

but have it sum the occurrences of 'something1' and 'something2' for
each productId.

I hope that makes sense. If not just ask and I'll clarify it.
Jul 20 '05 #1
3 2072
Jay Donnell wrote:
I want to be able to find out how many times multiple words occur for
each productId.


Does this do what you want?

SELECT productId, count(*) AS occurrences
FROM occurrence AS o INNER JOIN word AS w ON o.word_id = w.word_id
WHERE word_word IN ('something1', 'something2')
GROUP BY productId
ORDER BY occurrences

If you want it to subtotal by productId and by word, try this:

SELECT productId, word_word count(*) AS occurrences
FROM occurrence AS o INNER JOIN word AS w ON o.word_id = w.word_id
WHERE word_word IN ('something1', 'something2')
GROUP BY productId, word_word
ORDER BY occurrences

Regards,
Bill K.

Jul 20 '05 #2
Neither of those do what I'm looking for. Let me try to explain it
better.

There is a product table that contains the descriptions of the
products. Let's say that a description is this.
"SS Cubic Zirconia Bracelet 7;
Tennis bracelet in a league of its own. You won't find anything quite
like this stunning cubic zirconia tennis bracelet. Its figure styling,
lovely 18k gold (40 mils) over sterling silver setting and radiant
beauty sets it apart.
"

The word table contains every word that appears in all the
descriptions and the occurrence table contains an entry for every
instance of a word in all the descriptions. I.e. there is a unique
entry in the word table for each word and a seperate entry in the
occurrence table for every occurrence of that a word.

I want a query that will tell me the number of times a set of keywords
appears for each product. If the keywords are 'bracelet' and 'cubic'
'zirconia' then this description will have a count of 7. Bracelet 3
times, cubic 2, and zirconia 2.

I'm currently using this query which works for a single keyword.

SELECT productId, count(*) as occurrences
FROM occurrence AS o, word AS w
WHERE o.word_id = w.word_id and word_word = 'keyword' group by
productId order by occurrences desc limit 0, 650

I run it once for each keyword and sum them in the php code. I was
hoping that there would be a way to do this with a single query which
should be a lot faster.

I hope that helps explain it better.
Does this do what you want?

SELECT productId, count(*) AS occurrences
FROM occurrence AS o INNER JOIN word AS w ON o.word_id = w.word_id
WHERE word_word IN ('something1', 'something2')
GROUP BY productId
ORDER BY occurrences

If you want it to subtotal by productId and by word, try this:

SELECT productId, word_word count(*) AS occurrences
FROM occurrence AS o INNER JOIN word AS w ON o.word_id = w.word_id
WHERE word_word IN ('something1', 'something2')
GROUP BY productId, word_word
ORDER BY occurrences

Jul 20 '05 #3
Jay Donnell wrote:
I want a query that will tell me the number of times a set of keywords
appears for each product. If the keywords are 'bracelet' and 'cubic'
'zirconia' then this description will have a count of 7. Bracelet 3
times, cubic 2, and zirconia 2.


Yes, I have implemented similar designs. Given your description, I
still believe that this query gives you what you need:

SELECT o.productId, count(*) AS occurrences
FROM occurrence AS o INNER JOIN word AS w ON o.word_id = w.word_id
WHERE w.word_word IN ('bracelet', 'cubic', 'zirconia')
GROUP BY o.productId
ORDER BY occurrences DESC
LIMIT 650

I tried this out in my test database, given the sample data you gave,
and it does return a count of 7 as the value for occurrences.

Regards,
Bill K.
Jul 20 '05 #4

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

Similar topics

2
by: valexena | last post by:
Does PL/SQL allow cursors to contain subqueries? -- Posted via http://dbforums.com
6
by: pete | last post by:
Been banging my head against the wall with subqueries. Even simple stuff like this fails: SELECT CompanyName FROM tblcompanies WHERE CompanyName IN (SELECT HostName FROM tblhosts) Am I...
6
by: Daniel Elliott | last post by:
Hello, I was wondering if anyone would be able to help me with a problem I'm having. I'm trying to use the following query: SELECT Distinct c.site_id FROM campsite c WHERE c.site_id NOT IN...
1
by: karen | last post by:
i have a table that contains the fields order_id, canceled, and captured. the same order_id can appear many times in the table. i want to retrieve all the order_ids for which NO record exists...
5
by: Nick | last post by:
Im moving a development app (MySQL 5.0) to a different server which runs MySQL 4.0.20-standard. I am getting errors on queries that have subqueries such as... SELECT id FROM table1 WHERE id IN...
2
by: CSN | last post by:
Is there much difference between using subqueries and separating out them into separate queries? __________________________________ Do you Yahoo!? Yahoo! SiteBuilder - Free web site building...
2
by: orin | last post by:
Hi all, I've seen mention that you can use nested subqueries down to as many levels as you like but whenever I run the following: select * from table1 where tab1ID in (select tab1ID from...
0
by: DukeSnyder | last post by:
I am trying to divide 2 subqueries. there is a comment in the query pointing out what I have unsuccessfuly tried. Can anyone lead me in the right direction please. SELECT TOP 100...
0
debasisdas
by: debasisdas | last post by:
Using Subqueries ================== The sub query is often referred to as a nested SELECT, Sub - SELECT, or inner SELECT statement. The sub query executes once before the main query. The...
5
by: Element | last post by:
Execution of the following statement sends Access into permanent la-la land, but works fine in SQL Server on the same table: SELECT * FROM tblDailyProofTickets WHERE TicketID NOT IN...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.