SQL - Filtering search results - php

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)

Related

Get column names from one table and values from another table in MySQL and show in a tabular view

I have two different tables 'products' & 'prod_attributes'. I am storing product column names in one table and product values in another table. I am doing this, because I have different products data like lights, fans, computers etc. and their columns names are different for each product. Their structure is as follows:
prod_attributes table
id attr_name
1 Model No
2 Description
3 Watts
4 Lumen Output
5 TR Offer Lumen Output
6 Strip Lumen Output
7 Color Temp
8 Output Voltage
9 Certification
10 Warranty
11 Price
products table
id prod_attributes_id prod_attr_value
1 1 ABC9801
2 2 2FT LED T8 GLASS TUBE, DUAL END BYPASS
3 3 10
4 4 0
5 8 1200
6 9 3500K, 4000K, 5000K
7 9 UL, DLC
8 10 5 YEAR OR 50,000 WARRANTY
9 11 5.95
Now, I want to show the products list in a tabular view like below:
Model No Description Watts Lumen Output TR Offer Lumen Output Strip Lumen Output Color Temp Output Voltage Certification Warranty Price
ABC9801 2FT LED T8 GLASS TUBE 10 0 N/A N/A N/A 1200 DLC 5 Yrs 5.95
UDH2837 Ceiling Fan N/A N/A N/A N/A N/A 200 UL 1 Yrs 10.00
How can I write MySQL query to retrieve the data and show it in a tabular view in PHP.??

Need Help ranking the poll options based on prority selection

Need some help in ranking poll options, tried doing by multidimensional arrays but nothing worked.
My data structure is as per below:
**Table** Poll
**Id** **Question** **op1** p1 **op2** p2 **op3** p3 **op4** p4 **op5** p5
1 q1 ? Excellent 5 Better 4 Good 3 Ok 2 Not OK 1
2 q2 ? Sure 5 Perfect 4 Fine 3 Never 2 No 1
**Table** Answer
**id** **poll_id** **users_id** **answer** **resultOrder**
---------------------------------------------------------------
1 1 1 Excellent 1
2 1 1 Better 2
3 1 1 Ok 3
4 1 1 Good 4
5 1 1 Not Ok 5
6 1 2 Excellent 1
7 1 2 Ok 2
8 1 2 Better 3
9 1 2 Not Ok 4
10 1 2 Good 5
Each user will submit five options priorities them as per his suggestion
Option will get ranking as per counts
e.g Excellent selected for 2 times for first preference it will get 100 points and others will get 80,60,40,20 points based on their counts for all priorities.
if count ties matches points will be given on p1, p2 column in table poll
**For Example if my group by [answer] look like below 5 people voted Excellent as first preference, 4 people voted better as second preference and so on till Not Ok preferred by 1 person for fifth preference **
Answer count(answer) it should set point like this
Excellent 5 100
Better 4 80
Good 3 60
Ok 2 40
Not Ok 1 20
Hope this is possible. I tried it many ways, but no luck.

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.

Facted Search using php and mysql

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.

Count amount of rows in database with the same value in two columns - Use amount of top 5 rows to set width of 5 different divs

id company_id company_name size_of_company employee
-------------------------------------------------------------------
1 1 Comp 1 Big John
2 1 Comp 1 Big Ann
3 1 Comp 1 Big Peter
4 2 Comp 2 Big Lisa
5 2 Comp 2 Big Steve
6 3 Comp 3 Big Mike
7 3 Comp 3 Big Anna
8 3 Comp 3 Big Jon
9 3 Comp 3 Big Nick
10 3 Comp 3 Big May
11 4 Comp 4 Big Lee
12 4 Comp 4 Big James
13 4 Comp 4 Big Jess
14 4 Comp 4 Big Carrie
15 5 Comp 5 Big Luke
16 5 Comp 5 Big Brad
17 5 Comp 5 Big Joan
18 5 Comp 5 Big Ruth
19 5 Comp 5 Big Joel
20 6 Comp 6 Big Paul
This is probably an easy question, but my skills are quite low when it comes to MySQL and PHP. I have tried different loops with no result at all. Any help would be much appreciated.
What I want to do is this:
How do I count the amount of rows with the same company_id and size_of_company and then collect the amount of rows for the top 5 along with the names of the top 5 companies?
The amount of rows will be used in a div as width value for the div. The div shall be printed 5 times with different width value. The more rows the bigger div.
There will also be values of Medium and Small in size_of_company. But a company_name can only have one size_of_company. These two other size_of_company shall also be printed on the page, under their own category, Medium or Small.
If I understand the question correctly, the following would count the number of rows with the same company_id, company_name and size_of_company (using GROUP BY). Ordering and then limiting the number of results gives you the top 5.
SELECT
t.company_id,
t.company_name,
COUNT(*)
FROM
tble t
GROUP BY
t.company_id, t.company_name, t.size_of_company
ORDER BY
COUNT(*) DESC
LIMIT 5

Categories