Prevent duplicate row values - php

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.

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.

after multiple delete maintain order column

in my project i have to maintain a order of every record and if i delete in between i have to maintain order in a form that order don't have any gape like after 3 next is 4 only... not 5 or 6......my display like.
if i delete multiple record that have Rule ID are 1,3 and 4...then my rule ID 0 remain 0, 2 becomes 1, and 5 becomes 3...like that i want. i am deleting record base on id.
id rileid
1 0
3 1
5 2
6 3
11 4
14 5
how to do that? please help me.

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

Mysql select switching rows for every execution

I am developing auto bidding system,There is a tricky scenario. That is, I want select next machine to bid from "table_machine" on each execution of code.
Which means, I am picking bids from active (status=1) machine to bid.
execution 1 bidz from mid 1
execution 2 bidz from mid 2
.......
execution 4 bidz from mid 4
execution 5 bidz from mid 1
after execution 1 if user 2 bidz on live then machine bid should negleate mid 2 and its should bid mid3
I am having two tables
table_bid_history
history_id user_id product_id price type
1 1 1 10 live
2 2 1 10.5 live
3 1 1 11 machine
4 2 1 11.5 machine
5 3 1 12 machine
6 4 1 12.5 machine
7 1 1 13 machine
table_machine
mid user_id product_id start end num_bidz status
1 1 1 8 12 5 1
2 2 1 10 15 15 1
3 3 1 11 16 10 1
4 4 1 11 16 10 1
How can i do this , I am feeling this is tricky But there must be way to do this. give me idea please .
Please advise me ! Thanks
If i understand correctly you could do this :
create a new fake field on your machine_table and named it lastSelect and in your Where Clause Use something like this :
SELECT FROM .............
LEFT JOIN table_machine tm ON tm.lastSelect=1
WHERE id = IF(tm.id=MAX(tm.id),1,tm.id)
Then update current lastSelect on table_machine to 1

Categories