Mysql select switching rows for every execution - php

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

Related

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.

How to fetch value from table as per day using PHP and MySQL

I need one help.I need to fetch value from table as per day from a week using PHP and MySQL.I am explaining my table below.
db_comment:
id comment member_id comment_status day
1 Hii 52 1 1
2 Hii 52 1 2
3 Hii 52 1 3
4 Hii 52 1 4
5 Hii 52 1 5
6 Hii 52 1 6
7 Hii 52 1 7
Here my requirement is for same member_id from lower day(i.e-1) to higher day(i.e-7) if all comment_status value is 1 then true will return (lets say $isComment=true) with comment value (i.e-$comm=Hii) and if any of day comment_status value is 0 for same member_id then false value will return (i.e-$isComment=false) with comment value as blank(i.e-$comm=''). I am explaining my query below.
$sql=mysqli_query($connect,"select * from db_comment where member_id=52");
Please help me to fetch value as per explained above.

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

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