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

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.

Related

Get data from table only id 2 data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i hava a table
id sentUID recUID Amount
1 1 2 100
2 2 1 100
3 4 2 100
4 2 1 100
5 8 6 100
6 2 9 100
and i want to get userID 2 record like this
id sentUID recUID Amount
1 1 2 100
3 4 2 100
6 2 9 100
only three UserID 1, 4, 9 sent or recive amount.
I think you want:
select t.*
from t
where 2 in (sentUID, recUID)
order by id;
Try this:
SELECT * FROM data WHERE id IN
(SELECT tab1.mId FROM
(SELECT MIN(id) as mId, CASE WHEN sentUID=2 THEN recUID
WHEN recUID=2 THEN sentUID
END AS person
FROM data WHERE recUID=2 OR sentUID=2
GROUP BY person)
AS tab1);
result:
id sentUID recUID Amount
1 1 2 100
3 4 2 100
6 2 9 100
the intermediate table tab1 lists each of the unique UIDs that appear with 2 and is used to calculate the final result:
mId person
1 1
3 4
6 9
http://sqlfiddle.com/#!9/16f6a5/13/0

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.

Query database for most current message per receiver_id [duplicate]

This question already has answers here:
MySQL query, MAX() + GROUP BY
(7 answers)
Closed 6 years ago.
Here is example of my mysql query problem:
Table name: messaging
Id ad_id sender_id receiver_id message
1 2 5 1 message1
2 2 5 1 message2
3 2 5 1 message3
4 8 7 3 message4
5 2 4 2 message5
Now i would like to run a mysql query which will output results in following format:
Id ad_id sender_id receiver_id message
3 2 5 1 message3
4 8 7 3 message4
5 2 4 2 message5
Note: multiple rows with the same value in ad_id, sender_id and receiver_id should output only the last row.
I would appreciate any assistance to achieve similar output. Thanks in advance.
I suggest shortlisting the correct Ids before extracting the data, especially if you have more columns.
select Id, ad_id, sender_id, receiver_id, message
from messaging
where id in (select max(id)
from messaging
group by ad_id, sender_id, receiver_id)
You should use GROUP BY with the 3 columns you want to agroup and then use the MAX to get the last inserted row of each group (assuming the Id is auto-increment)
SELECT MAX(Id) as Id, ad_id, sender_id, receiver_id FROM messaging GROUP BY ad_id, sender_id, receiver_id;

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

Categories