Mysql SELECT for friends system - php

I have this table
conncections
id_user connectedto
1 4
2 4
3 1
1 5
i would like to do a SELECT that gives me back all the numbers that have a relation with the number 1 and the number 1 itself. Therefore 1 4 3 5 as result.

Something like
SELECT DISTINCT id_user as related
FROM connections
WHERE connectedto = 1
UNION
SELECT DISTINCT connectedto as related
FROM connections
WHERE id_user = 1
With UNION duplicate are removed

Related

How to fetch all values of a column based on value of another column in same table?

I got the following structure:
admin_id || country_id
---------------------------------
1 2
5 1
1 2
2 3
5 62
1 1
3 62
How to fetch all values by taking the $_SESSION['admin']['id'], finding the country_id of that admin and getting all other admin_id and country_id that are the same of the session admin?
So, lets say the currently logged in admin has id = 5 , that means the admin_id: 5 has two country_id: 1 and 62. I want to take all rows that have country_id: 1 and 62.
It should return this:
admin_id || country_id
------------------------
5 1
5 62
1 1
3 62
How can I do this in one sql query?
You can use a where clause for filtering on the admin_id or the country_id:
select t.*
from t
where t.admin_id = 5 or
t.country_id in (select t2.country_id from t t2 where t2.admin_id = 5);
SELECT t1.*
FROM table t1
JOIN table t2 USING (country_id)
WHERE t2.admin_id = 5
fiddle

" People Who Liked this Also Liked " Query in Mysql PHP

Music table
id | title
1 Rap God
2 Blank Space
3 Bad Blood
4 Speedom
5 Hit 'em up
Like table
u_id | m_id
1 1
1 2
1 4
1 5
2 3
2 4
2 5
3 1
3 5
4 1
4 2
4 5
Now if someone visits music with m_id = 1
Then the output might be like
m_id
5
2
4
To explain this a bit...
As m_id = 1 is liked by users -> {1,3,4} which in turn likes ->{2,4,5} musics. Since m_id=5 is liked by max number of users its first followed by m_id = 2 and m_id = 4.
My Try
I queried the users who liked m_id = 1
SELECT u_id FROM likes WHERE m_id =1
Then i stored in in an array and selected each of their likes and
arranged them in desc order of count.
But it is a very slow and long process is there any way i can do this ?
p.s I have heard of Association Rules and Bayesian theorem can be user to achieve this. But can anyone help me out with an example ?
You can JOIN back on the Like table and do something like this.
SELECT also_like.m_id, COUNT(also_like.m_id)
FROM [like] AS did_like
JOIN [like] AS also_like ON
also_like.u_id = did_like.u_id
AND also_like.m_id != did_like.m_id
WHERE did_like.m_id = 1
GROUP BY also_like.m_id
ORDER BY COUNT(also_like.m_id)
Essentially you are getting a list of users who liked an item then getting a complete list of those user's likes excluding the item they just liked.
You can then add a HAVING clause or LIMIT to filter things down a bit more.
using a subquery ...
SELECT m_id, count(u_id) as Rank FROM `like`
WHERE u_id in
(
SELECT u_id
FROM `like`
WHERE m_id = 1
)
AND m_id <> 1
GROUP BY m_id
ORDER BY Rank DESC
and optionally
LIMIT 0, 10
or how many "alsolikes" you want to display

Sql query to show count(*) of groups while keeping all rows of the group

I have a table similar the following
id user_id father_id
1 1 1
2 2 1
3 3 2
4 4 2
5 5 2
6 6 3
7 7 4
I search for a sql query (prefer Fluent or Eloquent for Laravel 4) to give me the following result:
id user_id father_id family_members
3 3 2 3
4 4 2 3
5 5 2 3
1 1 1 2
2 2 1 2
6 6 3 1
7 7 4 1
As it can be observed family_members is the count of users who have the same father_id
select id, user_id, father_id, count(*) as family_members from users group by father_id
The query above, just keeps the top row of each group, but I want to keep all the other records and not only the first one; then sorting them first according to the family_members and then according to father_id
How can I achieve it?
I donĀ“t know Fluent, nor Eloquent, nor Laravel 4, but the sql query would be like this.
SELECT yourTable.*, auxTable.family_members
FROM yourTable
LEFT JOIN
(
SELECT father_id, COUNT(id) as family_members
FROM yourTable
GROUP BY father_id
)auxTable ON yourTable.father_id = auxTable.father_id
ORDER BY family_members DESC, yourTable.father_id ASC
I solved the problem by the following Fluent query based on the answer from segio0983
$users = DB::table('users')
->leftjoin(DB::raw('(SELECT father_id, COUNT(id) as family_members
FROM users
GROUP BY father_id) auxTable '),function($join)
{
$join->on('users.father_id', '=', 'auxTable.father_id');
})
->orderBy('auxTable.family_members','desc')
->orderBy('users.father_id','asc')
->select('users.id','users.father_id','auxTable.family_members');

MySql Distinct with multiple fields

I have 4 columns a, b c, d in my table (MySQL database) . I want to select the distinct values of ALL of these 4 columns in my table . More deeply my table is given bellow..
a b c d
--------------------------
1 3 3 4
1 2 3 0
1 1 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
In the above table (1,2,3,4) value are repeating 4 times(look the last 4 rows of my table). I want only the distinct one , ie i want to get the bellow table after mysql query..
a b c d
---------------
1 3 3 4
1 2 3 0
1 1 3 4
1 2 3 4
I think you guys got some idea . Im not familier with MySql .
select distinct a,b,c,d from your_table
SELECT DISTINCT column_name,column_name FROM table_name;
I mean
select distinct a,b,c,d from table_name;
Here is the link of w3schools
use :
SELECT DISTINCT * FROM yourtable
try this:
SELECT DISTINCT a FROM my_table
UNION
SELECT DISTINCT b FROM my_table
UNION
SELECT DISTINCT c FROM my_table
UNION
SELECT DISTINCT d FROM my_table
update 1:
SELECT a,b,c,d
FROM my_table
GROUP BY a,b,c,d;
http://sqlfiddle.com/#!2/c49ad/11

Merge results from two different tables with different column number, ORDER them by same key

I have two tables:
Table A: ID Cars Planes Num
1 3 5 2
2 8 44 1
3 7 23 6
4 6 2 7
Table B: ID Horses Dogs Cats Elefants Num
1 3 5 2 3 3
2 8 44 1 22 4
3 7 23 4 14 8
4 6 2 3 15 5
What I need to do: I need to get all results from both tables and sort them by the "Num" Column, where the "number" actually is unique for each result from both rows.
Is it even possible to "merge" those two tables and order them by "num" or should I just get each table separately ordered and do two loops checking always for the next num jumping between tables?
Thanks
you can merge them like that with UNION .
try this:
select num from(
select num from table1
union all
select num from table2
)t
order by num asc
DEMO HERE
EDIT:
select id ,Cars,Planes, Horses,Dogs,Cats,Elefants,num from(
select id ,Cars,Planes,'No horses' Horses,'No dogs' Dogs,'No cats' Cats,'No elefants' Elefants,num from table1
union all
select id,'No cars' Cars,'No planes' Planes,Horses,dogs,Cats,Elefants, num from table2
)t
order by num asc;
DEmo with other columns
SELECT NUM FROM TABLEA
UNION ALL
SELECT NUM FROM TABLEB
ORDER BY 1

Categories