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: -
Product A Attribute 1
-
Attribute 2
-
Attribute 3
-
Product B Attribute 5
-
Product C Attribute 6
-
Attribute 7
-
what i did was..... -
SELECT *
-
FROM (SELECT * FROM Attributes where Attribute_Name LIKE '%...%') as A
-
join
-
(SELECT * FROM Products limit 0,10) as P
-
on (P.Id=A.Product_Id)
-
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
7 2009 Atli 5,058
Recognized Expert Expert
Hi, and welcome to TSDN!
You could try something like this: -
SELECT * FROM Products
-
WHERE Products.id IN(
-
SELECT Product_ID
-
FROM Attributes
-
WHERE Attributes LIKE '%...%'
-
)
-
LIMIT 10
-
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.
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?
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: -
SELECT * FROM Products
-
INNER JOIN (
-
SELECT Product_ID
-
FROM Attributes
-
WHERE Attributes LIKE '%...%'
-
GROUP BY Product_ID
-
LIMIT 10
-
) AS lt
-
ON lt.Product_ID = Products.id
-
LEFT JOIN Attributes
-
ON Products.id = Attributes.Product_ID
-
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~
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.
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 ?
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 ?
- select count (*) from selectedProducts
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
--
|
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...
|
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*'"
|
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
|
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.
| |
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..
|
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....
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |