473,507 Members | 2,477 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 1996
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
1329
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...
3
4188
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...
2
5574
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...
0
2450
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...
7
1481
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...
4
1357
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
1646
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
2286
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...
0
7321
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,...
0
7377
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
7488
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
5623
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,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
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...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...

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.