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
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have one table like this.
id conversation_id messageable_id
1 1 3 <--
2 1 5
3 2 7
4 2 3 <--
5 3 7
6 3 9
One conversation has two member.
If sender is 3, I have to get all conversation and receiver like following.
conversation_id sender_id recevicer_id [sic]
1 3 5
2 3 7
How can I build query?
Thanks in advance.
You could use lead() (available in MySQL 8.0):
select *
from (
select
conversation_id,
messageable_id sender_id,
lead(messageable_id) over(partition by conversation_id order by id) receiver_id
from mytable
) t
where sender_id = 3
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.
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)
)
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 8 years ago.
Improve this question
My timetable table looks like this:
id period mon mon_tch tue tue_tch wed wed_tch
-- ------ --- ------- --- ------- --- -------
1 prd1 4 5 8 7 6 3
2 prd2 6 3 4 5 8 7
My teacher-subject table:
id tchr subject
-- ---- -------
1 5 4
2 7 8
where values in mon is the subject_id and mon_tch is the teacher_id and so on.
When admin changes the subject of a teacher in the 'teacher-subject' table via a form (example: subject of teacher with id 5 is changed from 8 to 9), I want to update my timetable table with the new subject assigned.
(consider the subject field in the teacher-subject table will be updated somehow).
A normalised design might look like this...
period day subject teacher
--------------------------
1 mon 4 5
1 tue 8 7
1 wed 6 3
2 mon 6 3
2 tue 4 5
2 wed 8 7
... where (period,day) constitutes a compound PK. That said, there may still be some redundancy here.
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.