Delete duplicate values in column in display data - php

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.

Related

PHP + MySQL - ORDER BY LIKES

So I am using mysql to store likes on Minecraft Server Pages as a way for them to advertise. However, I am storing these likes in a separate table:
id user_id server_id
1 1 1
2 1 24
4 2 22
5 2 22
6 2 1
7 2 4
8 2 5
9 2 6
10 2 17
11 2 18
12 2 21
13 2 24
Insert code:
INSERT INTO likes (user_id, server_id) VALUES ('".$user_id."', '".$server_id."');
But I am currently sorting them by date added (recent first) but I would like to know how to sort them by likes.
You should put a counter column on your servers table (lets say: likes_counter) and then, every time you add a like to the likes table, you increment this counter. Put a index on that column tooo!
INSERT INTO likes (user_id, server_id) VALUES ('".$user_id."', '".$server_id."');
UPDATE servers_table SET likes_counter = likes_counter+1 WHERE server_id = '".$server_id."';
Now, when you select the servers, you only need to do a ordered by likes select:
SELECT * FROM servers_table ORDER BY likes_counter DESC;
Here i'm assuming your table of servers is named servers_table, replace it with the name you gave to the table.

mysql advanced query select based on params

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)
)

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.

MySQL SUM only 1 field and

I have the following table structure
--ID-- --Date-- --Value--
1 2013-1-2 23
2 2013-1-2 11
3 2013-1-3 8
4 2013-1-3 7
As you can see the dates can overlap and I want to output every different date with a summation of the values attributed. So for this it would be.
--Date-- --Total--
2013-1-2 34
2013-1-3 15
Is this even possible with a query or will I have to do some seperate summation?
SELECT Date, sum(Value) as Total
FROM Table
GROUP BY Date

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.

Categories