473,796 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

limit on results with a "search criteria"

11 New Member
hi experts,
the topic is rather misleading but i couldn't find a better way to phrase it.. here's my scenario:

Table A: Products
Table B: Attributes (with a product_id to show which product it belongs to)

i need to join the 2 tables to get what i require, which is a html like so:

Expand|Select|Wrap|Line Numbers
  1. Product A       Attribute 1
  2.                 Attribute 2
  3.                 Attribute 3
  4. Product B       Attribute 5
  5. Product C       Attribute 6
  6.                 Attribute 7
  7.  
what i did was.....
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM (SELECT * FROM Attributes where Attribute_Name LIKE '%...%') as A
  3. join
  4. (SELECT * FROM Products limit 0,10) as P
  5. on (P.Id=A.Product_Id)
  6.  
the problem is when i require the user to select his search criteria, i cannot append my sql to match the desired results. I want to allow the user to list only Products with Attributes that matches his, and at the same time limit it to 10..

As with my statement, it gets the 1st 10 products, and joins with attributes that contain the search... which doesnt mean i will have 10 unique products based on the attributes...

hope my scenario was described well enough.. thanks
Sep 7 '07 #1
7 2009
Atli
5,058 Recognized Expert Expert
Hi, and welcome to TSDN!

You could try something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM Products
  2. WHERE Products.id IN(
  3.     SELECT Product_ID 
  4.     FROM Attributes 
  5.     WHERE Attributes LIKE '%...%'
  6.   )
  7. LIMIT 10
  8.  
Which would return the first 10 products that contain an attribute that matches the keyword.

If you would rather have the first 10 attributes along with their product info, you could simply add a join to this query.
Sep 7 '07 #2
veliscorin
11 New Member
if i do it that way, i will only get the set of products which contain the attributes matching my criteria

that is to say... 5 unique products if only 5 match....
so.. does it mean i can join the Attributes table so that i can get the Attributes that are associated with these 5 products too?
Sep 7 '07 #3
Atli
5,058 Recognized Expert Expert
The LIMIT at the end of the query limits the amount of rows it returns to 10. So if you were to join the Attributes table with this query it would return the first 10 Attributes that match your search terms.
So if 6 Attributes in each of the first two Products matched the search terms, you would get the first 10 Attributes along with their Product info and nothing more.

If you want 10 Unique Products and all the Attributes associated with them you will have to do something different.
For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM Products
  2. INNER JOIN (
  3.     SELECT Product_ID 
  4.     FROM Attributes 
  5.     WHERE Attributes LIKE '%...%'
  6.     GROUP BY Product_ID
  7.     LIMIT 10
  8.   ) AS lt
  9.   ON lt.Product_ID = Products.id
  10. LEFT JOIN Attributes
  11.   ON Products.id = Attributes.Product_ID
  12.  
Sep 7 '07 #4
veliscorin
11 New Member
Hi Atli~

that worked for me yea~
wow i couldn't have thought of it by myself seriously..
i'm quite inexperienced when coming up with queries

thanks alot by the way~
Sep 8 '07 #5
Atli
5,058 Recognized Expert Expert
Glad I could help :)
Good luck with your project! And don't hesitate to start a new thread if you have any more questions or problems we can help with.
Sep 8 '07 #6
veliscorin
11 New Member
as a continuation, if i needed a count of the selected products, how am i able to do so?

where do i place my COUNT syntax ?
Sep 9 '07 #7
r035198x
13,262 MVP
as a continuation, if i needed a count of the selected products, how am i able to do so?

where do i place my COUNT syntax ?
Expand|Select|Wrap|Line Numbers
  1. select count (*) from selectedProducts
Sep 10 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1350
by: Leu | last post by:
I like to search in the forum but I can't find the (in FAQ described) "search" link: "... To access the search feature, click on the "search" link at the top of most pages. ..." Where is the "search" link? Leu --
3
4208
by: Alastair | last post by:
Hello guys, I've been building a search facility for an intranet site I'm part of developing and we've been building a search engine using Index Server. It mostly works, however there have been a few niggling problems and fixing it seems to be a case of patching errors as we find them so I'm thinking that it might be worth starting the logic from scratch and rebuilding this again. Basically we have a simple search, which is simply a...
2
5605
by: Adam Short | last post by:
I've never needed it before but now I do! Does anyone know if you are able to perform a NOT Like search using Classic ASP ADO? i.e. myData.Filter = "Ref NOT LIKE '*1234*'" by the way this doesn't work!! and neither does myData.Filter = "Ref <> LIKE '*1234*'"
0
2474
by: Greg Strong | last post by:
Hello All, In the past I've used a combo box with the 'row source' being an Access SQL union query to select "All" or 1 for only 1 criteria in a query. An example is as follows: SELECT 0 As PvdrID, "All" As PvdrDesc FROM tblPrvdr UNION SELECT PvdrID, PvdrDesc FROM tblPrvdr ORDER BY PvdrID; In the query I would place a field which wasn't selected for display which was
7
1499
by: Matt Kruse | last post by:
Javascript "Knowledge Base" Search: http://www.javascripttoolbox.com/search/ Responses to questions on this group are often along the lines of "did you read the FAQ" or "did you search the archives?" or "did you do a google search?". To regular readers and experienced javascript developers, knowing how and where to search and filtering out junk results is an easy task. For most javascript users, however, it's not so easy.
4
1373
by: rempit | last post by:
In 1 form..have 2 "DIV".. "DIV" one is for SEARCH.. "DIV" two is for showing the RESULT from Database of "DIV" one Button.. Everything in one page.. Anyone can help me please..
1
1658
by: charliemears | last post by:
Hi there... How can I limit search results so that there is a "Next 10" and "Previous 10"? thanks for any reply....
1
2305
by: Nour469 | last post by:
Hello, I have a problem when I run search, I have fields for: name, clientID and phoneNo. When a client calls I need to find them in the database, they might give me the name and it might not be spelled correctly, I need to search by phoneNo. (the fastest way!) I place the cursor in the appropriate field and click search. when the find window opens up, by default, the Look in: field is: 'Main application form: form' and the 'search field...
0
10459
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
10237
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
10187
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
10018
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
9055
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
5446
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
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.