Multiple tables and queries - php

How do I join something like this together:
$query = ("SELECT * FROM profiles,follow
WHERE follow.friend ='$user'
AND follow.user !='$user'
AND profiles.user1 =follow.user
ORDER BY id DESC limit 20");
or
("SELECT * FROM profiles WHERE user1='$user'");
I need the query to display two different things how do I do that.

This one?
SELECT
*
FROM
follows
INNER JOIN
profiles
ON (profiles.user1 = follow.user)
WHERE
follow.friend = '{$user}'
AND follow.user != '{$user}'
AND profiles.user1 = '{$user}'
ORDER BY
profiles.id DESC
LIMIT 20

Use UNION:
SELECT *
FROM profiles, follow
WHERE follow.friend ='$user'
AND follow.user !='$user'
AND profiles.user1 =follow.user
ORDER BY id DESC limit 20
UNION
SELECT *
FROM profiles
WHERE user1='$user'

try this
$query = "SELECT * FROM profiles,follow WHERE follow.friend ='$user' and follow.user !='$user' and profiles.user1 =follow.user and profiles.user1='$user' order by id DESC limit 20";

Related

How do i use DESC limit 3 when i have joined two tables

I have joinend two tables and it workes like it should do but now i want to use DESC limit, but i dont know where i should put it
$query = 'SELECT * FROM posts INNER JOIN category ON posts.catid=category.id;';
$select_all_posts_query = mysqli_query($link, $query);
$query = 'SELECT * FROM posts INNER JOIN category ON posts.catid=category.id ORDER BY posts.catid=category.id DESC LIMIT 3 ';
$select_all_posts_query = mysqli_query($link, $query);
If you only want to limit your results you can just add LIMIT 3 to your query:
$query = 'SELECT * FROM posts INNER JOIN category ON posts.catid=category.id; LIMIT 3';
$select_all_posts_query = mysqli_query($link, $query);
Alternatively, if you are also trying to order your results is descending order you'll need to add and ORDER BY to the query as well:
$query = 'SELECT * FROM posts INNER JOIN category ON posts.catid=category.id; ORDER BY posts.id DESC LIMIT 3';
$select_all_posts_query = mysqli_query($link, $query);

Order a table by another table column?

I have this query:
$q = "SELECT * FROM user
WHERE sec='1' AND reg_by='".$_SESSION['login_username']."'
ORDER BY date DESC LIMIT $startrow, 30 ";
I have another table which stores appointments, it has a column named meet.
How can I sort this query by meet?
Not all data at users are in other table.
You can use the below query. Replace another_table with your original table name :
$q = "SELECT u.* FROM user AS u LEFT JOIN another_table AS at ON u.userid = at.userid WHERE u.sec='1' AND u.reg_by='".$_SESSION['login_username']."' ORDER BY at.meet DESC LIMIT $startrow, 30 ";
you can use joining for this like
select user.*,meet.* from user left join meet on (meet.userid = user.id) where user.sec='1' AND user.reg_by='".$_SESSION['login_username']."' order by meet.userid DESC
$q = "SELECT * FROM user INNER JOIN user
ON meets.userid=user.userid WHERE sec='1' AND reg_by='".$_SESSION['login_username']."' ORDER BY date DESC LIMIT $startrow, 30 ";

Writing a query that returns the latest row related to a specific user

Trying to create a query that gets the most recent record related to the user. Here's my attempt
"SELECT *
FROM (
SELECT *
FROM mytable
ORDER BY id
DESC LIMIT 1)
WHERE userid = $userID";
You could also:
Select * from table
Where id =
(Select Max(Id) from table
where userid = $userID)
Simply skip the sub-query:
SELECT *
FROM mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1
Use Top(1)
Select TOP(1) * FROM table ORDER BY id DESC
Try:
SELECT *
FROM mytable
WHERE userid = $userID
ORDER BY id DESC
LIMIT 1

MySQL select grouping multiple rows from same table

I'm trying to create a query to select multiple rows from the same table grouping them like an array.
Now i'm selecting them using php like this:
$tks = mysqli_query($con,"SELECT * FROM hof ORDER BY tks DESC LIMIT 5");
$top_ths = mysqli_query($con,"SELECT * FROM hof ORDER BY ths DESC LIMIT 1");
$top_tha = mysqli_query($con,"SELECT * FROM hof ORDER BY tha DESC LIMIT 1");
----
I would like to merge them in a single query so i get an associative array.
Something like this:
(SELECT * FROM hol ORDER BY tks DESC LIMIT 5) AS tks
UNION
(SELECT * FROM hol ORDER BY ths DESC LIMIT 1) AS top_ths
So tks contains all the 5 rows and top_ths contains 1 row.
Is it possible ? Thanks.
to undersatnd from wich group row is, make additional field
(SELECT *, 1 as `group` FROM hol ORDER BY tks DESC LIMIT 5)
UNION
(SELECT *, 2 as `group` FROM hol ORDER BY ths DESC LIMIT 1)

Mysql get three sets of rows depending on field

I want three sets of rows in my query, with 100 in each. Each set with a different data value. So like this:
$result = mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100") or die(mysql_error());
$result .= mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100") or die(mysql_error());
$result .= mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100") or die(mysql_error());
How can I get the result in one query?
Thanks.
use UNION:
SELECT * FROM tableA UNION SELECT * FROM tableB UNION SELECT * FROM tableC
Use union
$final_query=$query1." union ".$query2." union ".$query3;
and
mysql_query($final_query);
Try Union Like
SELECT * FROM tableA UNION SELECT * FROM tableB UNION SELECT * FROM tableC
If you want to allow duplicate as well then try UNION ALL like
SELECT * FROM tableA UNION ALL SELECT * FROM tableB UNION ALL SELECT * FROM tableC
you can use UNION to do this work
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100)
One way is to use UNION.
$result = mysql_query("
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100)
");
More options here.

Categories