mysql advanced query select based on params - php

As first please be nice to me im a beginner with SQL language and english language.
So i have problem with this query.
I've created sqlfiddle.
My query look like it's working properly but I find it is not working
I would like to write a query that returns the product ID variant based on the parameters that will send
Correct query result would look like the following
PARAM = 6
id product_id productvariant_id attributevalue_id
1 3 1 6
2 3 2 6
---- BAD RESULTS -----
3 3 3 6
4 3 3 9
6 3 5 6
7 3 6 6
8 3 6 11
PARAM = 6,9
id product_id productvariant_id attributevalue_id
3 3 3 6
4 3 3 9
---- BAD RESULTS -----
3 3 3 6
4 3 3 9
6 3 5 6
7 3 6 6
8 3 6 11
What i really need is return productvariant_id which contains combination of inserted params, if I send only one attributevalue_id, i need to find productvariant which contain only ONE attributevalue_id. IF i send two params i finding combination of two.. not more or less

Unfortunately I do not have enough reputation for a comment, thus I have to write this as answer.
Could you clarify your goal?
Do you want to retrieve the productvariant_id of all datasets where the attributevalue_id matches your parameter?
SELECT productvariant_id
FROM productvariantattribute
WHERE attributevalue_id IN ($YOUR_PARAMETER_LIST);
Or do you want to retrieve the productvariant_id of all datasets where the productvariant_id has only these attributevalue_ids you specified as parameter?
SELECT `productvariant_id`
FROM `productvariantattribute`
WHERE `attributevalue_id` IN ($YOUR_PARAMETER_LIST)
AND `productvariant_id` NOT IN (
SELECT `productvariant_id`
FROM `productvariantattribute`
WHERE `attributevalue_id` NOT IN ($YOUR_PARAMETER_LIST)
)

Related

Delete duplicate values in column in display data

I have privot table that have position_id and provider_id. How delete duplicate values that table can look like second table. I would like solve this task by eloquent.
First table display next code:
MyTable::with('provider')->with('position')->orderBy('position_id')->get()
position_id provider_id
7 1
7 2
7 3
9 5
9 6
10 7
position_id provider_id
7 1
2
3
9 5
6
10 7
You might try:
MyTable::with('provider')->with('position')->orderBy('position_id')-
>groupBy('position_id')->get()
You may need to reverse the orderBy and groupBy.

percent match within the same table

Below I have an MySQL database where "id" is just an id for each row. "question" shows the id of the question. There are four questions, 11, 12, 13 and 14. For every question the user has four answer options (1,2,3 and 4) which is stored in the "answer" column. The "user" column indicates what user who answered. In this example we have user 10, 11 and 12
id question answer user
1 11 2 10
2 12 2 10
3 13 3 10
4 14 4 10
5 11 2 11
6 12 2 11
7 13 4 11
8 14 1 11
9 11 2 12
10 12 2 12
11 13 1 12
12 14 1 12
Let's say that user 10 is the reference user which means that I want to know how well user 10 matches with the others. Using SQL and/or php code how can I match the answers of the users such that I get the matches in percent with the highest percent shown first. So in this example I'm looking for something like.
user percent
1 11 50%
2 12 75%
I'm not sure if this is possible all the way with only SQL. Maybe a bit of php is needed to convert the count to % for instance.
Finally i got your desired output
Try this:
SELECT * ,round(count(*)/(SELECT count(*) FROM `list` where user=10)*100) as
pecentage FROM `list` as l where l.answer=(SELECT m.answer FROM `list` as m
where m.user=10 and l.question=m.question) group by (l.user) order by pecentage
It will produce out put as
and you can add l.user!=10 in where condition if you don't want user 10 value.

How to get the most occurred database rows with php [duplicate]

This question already has answers here:
Select most common value from a field in MySQL
(3 answers)
Closed 8 years ago.
I don't know how to phrase this, but I'm trying to get the rows with the most occurrences in the column to display, for example
ID from_id to_id
1 1 3
2 1 3
3 1 3
4 1 3
5 2 3
6 3 3
7 3 3
8 4 3
9 4 3
I'm trying to get 1,4,3 from the database because it's more frequent, how would I do this?
Sorry if it's a bad question, I don't know how to phrase it
This is quite easy, just COUNT(from_id), eg like this
SELECT from_id, COUNT(from_id) AS total FROM your_table
GROUP BY from_id
ORDER BY total DESC
You can now modify this, eg LIMIT 10 to get the top 10 or WHERE total > 1 before the GROUP BY to only get rows which occure more than once.

Prevent duplicate row values

I've done some digging and I can't find an effective way to prevent duplicate entries based on my needs. I need columns 2 (proj_id) and column 4 (dept_id) never to be the same, as each dept would only work on a project once. So, rows 1 and 4, 6 and 7, and 14 and 15 shouldn't be allowed. I'll keep digging as well.
summary_id proj_id hours_id dept_id date_entered
1 8 3 6 9/9/2012
2 2 2 6 9/9/2012
3 1 6 19 9/9/2012
4 8 3 6 9/9/2012
5 2 5 17 9/9/2012
6 7 2 5 9/9/2012
7 7 2 5 9/9/2012
8 2 5 17 9/9/2012
9 7 4 17 10/10/2012
10 3 6 1 10/10/2012
11 5 1 15 10/10/2012
12 4 4 3 10/10/2012
13 3 5 1 10/10/2012
14 8 2 13 10/10/2012
15 8 2 13 10/10/2012
Before applying unique combine key to your table, you have to remove duplicate records first then apply the following sql command:
ALTER TABLE your_table_name ADD UNIQUE (proj_id, dept_id);
Define an unique key on both columns
ALTER TABLE `your_table` ADD UNIQUE (`proj_id`, `dept_id`);
Looks like you are new to php and mysql. So here's the easiest way to do it.
Log on to PHPMyAdmin
Select your DB and your table.
View the Structure of it (clicking the button on top of the screen).
Check the two fields (proj_id and dept_id) using the check box on the left.
At the bottom of the table you should find the the words "With selected" and in front of it some actions. Select the option to make "Primary".
Of course, if you have duplicate entries first delete them.

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