Facted Search using php and mysql - php

I'm trying to implementing faceted search in a Jewellery store, but failed. Problem is when try to filter the attributes table. structure is as follows:
Products Table:
Id Product_Code Product_Name
1 ABCGOLD1GM 1 gm Gold
2 ABCGOLD2GM 2 gm Gold
3 ABCGOLD394 3.94 gm Gold
Attributes Table:
Id Attr_Name Alias
1 Metal metal
2 Fineness fineness
3 Weight weight
Product_Attributes Table:
id product_id attr_id value
1 1 2 9999
2 1 3 1 gm
3 2 2 9999
4 2 3 2 gm
5 3 3 3.94 gm
6 3 2 9167
Now i want to filter if someone selects 1 gm with 999 and 3.94 gm. If you know faceted search then you can easily know what i mean. I want to make query to do the same with the table structure above.

Related

Lucky Draw Concept : SQL Query to get random option based on percentage of appearance

I am developing a Lucky draw concept as following
There will be multiple options at database we have to get the random options according to percentage of appearance
Database table is as following
option_id | option_name | option_type | option_in_day(%) | option_status
----------------------------------------------------------------------
1 $2 Off 2 100 1
2 $3 Off 2 95 1
3 $4 Off 2 95 1
4 $5 Off 2 90 1
5 $8 Off 2 90 1
6 $60 Cashback 2 10 1
7 $100 Cashback 2 5 1
8 Umbrela 2 50 1
9 Iphone 2 2 1
10 International
Tour 2 1 1
11 Fidget
Spinner 2 70 1
12 Free
membership 2 30 1
13 Samsung S8 2 10 1
14 $20 Off 2 60 1
15 Travel Card 2 50 1
16 Soft Toys 2 70 1
Now from this table I want to get random 8 option according to percentage.
Less percentage option chance to retrieve in result will be less.
I have tried with random function in sql but can't reach the requirement.
This is how you should be able to do it:
set #percentage = 100 * rand();
select *
from table_name
order by table_name.percentage >= #percentage, rand()
limit 0, 8;
This query stores a random percentage into a variable. Then, we order the query by two criterias. The first criteria is that the percentage of the table is higher or equal to the percentage randomized. This makes sure that if there are elements both above and below the randomized percentage, then the elements above will be preferred. Elements having similar result in the first ordering criteria will be randomly ordered.

Auto complete search from multiple tables

Eg:
**Category Table**
Catg_id Catg_name
-------------------
1 Bike
2 Car
**Company Table**
Company_id Company_name
--------------------------
1 Bajaj
2 Honda
**Company_category table**
com_catg_id Company_id Category_id
---------------------------------------
1 1 1
2 2 1
3 2 2
** Models table**
Model_id Model_name com_catg_id
----------------------------------------
1 Pulsar 220 1
2 Unicorn 2
3 City 3
**Purchase Table***
Purchase_id Vehicle_No Rate model_id status
-------------------------------------------------------
1 KL 02 AN8306 50000 2 0
2 KL 10 AZ4764 120000 1 1
3 KL 04 AV8578 800000 3 1
These are 4 Database tables using.
I am using ajax for auto complete searching through a single field
eg: searching car, want to list all cars in purchase table of status 1
if searching bike, want to list all bike in purchase table of status 1
search using company name, want to list all vehicle from that company in purchase table of status 1
same as search using model name, vehicle no,rate want to list matched items in purchase table
Please help me and please send a mysql query for implementing this.
Check this tut
This would surely help you.
This tutorial is for single field. It isn't hard to modify the code and use for multiple fields

A different logic in ranking

I have to find a solution for my ranking scenario.
Say i have 100 users in database. And there is 5 subject in another table. In my project users have an option to rate 1 to 5 rating of any subjects. Means a user should have 5 different rating for 5 different subject. I need to shoe on the front page which is the most rated subject on the landing page.
I have a plan like create a table like
table name: user_subject_rate
Structure
id: user_id: subject1: subject2: subject3: subject4: subject5
1 1 3 1 2 5 4
When i create a table like this what happens if a new subject come to rate?
Can anybody suggest me a solution for this? I am using mysql as database.
I would advise you to structure your tables like below.
users
user_id | name
-----------------
1 Michael
2 Andrew
3 Annie
subjects
subject_id | name
----------------------
1 Maths
2 English
3 Physics
4 Chemistry
5 Biology
users_subjects_scores
user_id | subject_id | score
----------------------------
1 1 5
1 2 5
1 3 4
1 4 2
1 5 3
2 1 1
2 2 2
2 3 4
2 4 5
2 5 3
Then you can work out the total score of each subject using this query:
SELECT
name,
COALESCE(SUM(score), 0) AS total_score
FROM
subjects
LEFT JOIN
users_subjects_scores USING (subject_id)
GROUP BY
subject_id
ORDER BY
SUM(score) DESC
Then adding a new subject is as simple as adding a new row to the subjects table.
You can see an SQL fiddle here.

Display each row one by one from each category one by one using php mysql

i have the following table structure: basically it is grouping by agency_id.
id agency_id price type
1 1001 10000 A
2 1002 13000 B
3 1001 16000 C
4 1003 11000 A
5 1002 12000 C
6 1003 9000 D
7 1001 15000 A
8 1002 12000 A
i want to display it as following
id agency_id price
1 1001 10000
2 1002 13000
4 1003 11000
3 1001 16000
5 1002 12000
6 1003 11000
i am using the following code, but its showing all the properties from 1 agency, then 2nd agency and so on.
ORDER BY
CASE properties.agency_id
WHEN 1001 THEN 1
WHEN 1002 THEN 2
WHEN 1003 THEN 3
END
but its not giving me the desire results. any help will be apriciated
Regards,
Use a solution like the one in Ranking by Group in MySQL to add a rank column that increments within each group, and put that in a subquery. Then do:
SELECT *
FROM (subquery) AS x
ORDER by rank, agency_id

SQL - Filtering search results

OK – need some help here – might have bitten off more than I can chew here – but I’m looking to write a SQL query that always returns a minimum of 10 results. If the first condition is applied and the result set exceeds 10 results then move on to the next condition etc..
Example
Find all the small red plastic toy cars that cost less than £5.00.
I always want to show a minimum of 10 items on the screen. Want to search on “small” then on “red” and then on “< £5.00”. If the query returns more than 10 items then continue to filter on as many tags as possible (i.e plastic, toy and car) – the more matches it can make the higher it should be ranked (i.e if a products has all 3 tags associated with it – it will be at the top of the list, if a product only matches 1 tag – then this will be lower down the list.
Price Table
ID Price
1 £1.50
2 £2.50
3 £6.00
.
Colour Table
ID Colour
1 Red
2 Blue
3 Yellow
.
Size Table
ID Shape
1 Small
2 Medium
3 Large
.
Products Table
ID Description price_id colour_id size_id
1 Item 1 1 2 2
2 Item 2 2 2 1
3 Item 3 1 1 1
4 Item 4 3 2 3
5 Item 5 3 1 2
6 Item 6 1 1 2
7 Item 7 1 1 3
.
Tags Table
ID Description
1 Shiny
2 Plastic
3 Wood
4 Toy
5 Disney
6 Animal
7 Car
.
Items_Tags Table
ID tag_id product_id
1 1 1
2 4 1
3 7 1
4 2 2
5 3 3
6 4 3
7 7 6
8 7 7
9 7 2
Quite a long example - but I hope you get the point. I have wondered whether there would be any benefit to putting all the filters within the tags table - i.e the price, colour and size and then would only have to search against the tags table.
Any ideas anyone?
Thanks
It depends on your usage: filtering integers is faster than string (by the way, are they indexed?) but JOIN is also time consuming. So if the code outside the sql works with the integers, keep them (and don't join if not required to show the text)

Categories