How to merge two mysql results in php? - php

As this question clearly means that, I am not asking about SQL joins here,
I just want to combine/merge 2 MySQL result in PHP, i have tried to do it with PHP array_merge() but no success.
$user_paid_query = "SELECT * from users WHERE now_paid!=0 ORDER BY now_paid DESC";
$result_user_paid = $connect->query($user_paid_query);
$users_paid = $result_user_paid->fetch_assoc();
$users_unpaid_query = "SELECT * from users WHERE now_paid=0 ORDER BY id ASC";
$users = array_merge($users_paid, $users_unpaid);

You can use union in MySQL itself(instead of PHP) to merge 2 SQL results.
(SELECT * from users WHERE now_paid != 0 ORDER BY now_paid DESC)
UNION
(SELECT * from users WHERE now_paid = 0 ORDER BY id ASC)

Related

How to get ORDERED and limited rows form MySQL and randomize show order

Is possible, and how to ask MySQL for
SELECT * FROM my_table ORDER by row_id DESC LIMIT 8
get the last 8, newest record from my table, with randomized order for PHP showing method
$results = $mysqli->query($query);
while($row = $results->fetch_assoc()) {
echo $row['my_col_name'];
}
Colud I, and where put the rand() in my SQL query?
Without randomize I get last 8 rows ORDERED 10,9,8,7,6,5,4,3
I want to get in the following order:
9,7,5,4,6,10,3,8;
8,7,3,6,10,9,5,4
...
You can place it inside another select:
SELECT * FROM (SELECT * FROM my_table ORDER by row_id DESC LIMIT 8) t ORDER BY RAND()
Use a subquery:
SELECT t.*
FROM (SELECT t.*
FROM my_table t
ORDER by row_id DESC
LIMIT 8
) t
ORDER BY rand();

SQL Order By id and Count star not working

I would like to get number of all records and get last record :
$sql_count_sms = "SELECT count(*) as total,content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = $row_num_sms['total'];
$last_my_sms = $row_num_sms['content'];
I can get number of records but I can't get last content record .
It returns first record !
Where is my wrong ?
Below codes works fine, but I think count(*) is faster than mysql_num_rows .
$sql_count_sms = "SELECT content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = mysql_num_rows($result_count_sms);
$last_my_sms = $row_num_sms['content'];
Any solution?
The grain of the two results you want is not the same. Without using a sub-query you can't combine an aggregate and a single row into the same result.
Think of the grain as the base unit of the result. The use of GROUP BY and aggregate functions can influence that "grain"... one result row per row on table, or is it grouped by user_id etc... Think of an aggregate function as a form of grouping.
You could break it out into two separate statements:
SELECT count(*) as total FROM android_users_sms WHERE user_id = :id;
SELECT * FROM android_users_sms WHERE user_id = :id ORDER BY id DESC LIMIT 1;
Also, specific to your question, you probably want a LIMIT 1 in combination with the ORDER BY to get just the last row.
Now, counter intuitively perhaps, this should also work:
SELECT count(*), content, id
FROM android_users_sms
WHERE user_id = :id
GROUP BY id, content
ORDER BY id
LIMIT 1;`
This is because we've changed the "grain" with the GROUP BY. This is the real nuance and I feel like this could probably be explained better than I am doing now.
You could also do this with a sub query like so:
SELECT aus.*,
(SELECT count(*) as total FROM android_users_sms WHERE user_id = :id) AS s1
FROM android_users_sms AS aus
WHERE user_id = :id ORDER BY id DESC LIMIT 1;

Order by: Difference between two columns

i have a question about a sql query. I have two columns in table users:
Moneytotal and Moneythisweek
Right now i would like to order the results on difference.
How bigger the difference between Moneytotal and Moneythisweek the higher on the ranking.
Normally i use:
$lsel_rank = mysql_query("select * from users ORDER BY Moneytotal DESC");
$rank = mysql_fetch_array($lsel_rank);
But now i want to do something like:
lsel_rank = mysql_query("select * from users ORDER BY Difference between Moneytotal
AND Moneythisweek DESC");
Can anyone help me?
SELECT * FROM `users` ORDER BY Moneytotal - Moneythisweek DESC
Just simply use this
$lsel_rank = mysql_query("select *,'moneytotal - monethisweek' as difference from users ORDER BY difference DESC");
Option 1: (Doing calculation in orderby)
SELECT * FROM `users` ORDER BY (Moneytotal - Moneythisweek) DESC;
Option 2: (Using numbered index in order by and calculating the difference and make available in the select query's resultset
SELECT (Moneytotal - Moneythisweek) difference, a.* FROM `users` a ORDER BY 1 DESC;

SQL - concatenate results from 2 tables

First I need to get exact match like
SELECT * FROM movies WHERE title='STRING' ORDER BY x DESC
and then append to these results query with LIKE match
SELECT * FROM movies WHERE title LIKE '%STRING&' AND title<>'STRING' ORDER BY x DESC
and limit these results with maximum of 10 results.
UNION wont`t do the jobs as it sorts all results together and returns wrong order (I need exact match first, then with LIKE)
SELECT * FROM movies WHERE title='STRING' UNION
SELECT * FROM movies WHERE title LIKE '%STRING%' ORDER BY x DESC LIMIT 10
The best solution I got is to use multi_query()
$query = "SELECT * FROM movies WHERE title='STRING' ORDER BY x DESC; ";
$query .= "SELECT * FROM movies WHERE title LIKE '%STRING%' AND title<>'red' ORDER BY x DESC";
$Dbi->multi_query($query);
do {
$sql = $Dbi->store_result();
while($x = $sql->fetch_array()) {
...
}
} while($Dbi->next_result());
but in this case it is not possible to use any mysql inside the inner loop and there also must be better looking solution!
You can do this with one query, by using the order by clause:
SELECT *
FROM movies
WHERE title like '%STRING%'
ORDER BY title = 'STRING' desc,
title like '%STRING%' desc
LIMIT 10;
The first clause in the ORDER BY puts the exact matches first. The second then orders by the partial matches. The WHERE clause ensures that there is a match of some kind.
You don't need the UNION, it's accessing the same table twice:
SELECT *
FROM movies
WHERE title LIKE '%STRING&'
ORDER BY CASE WHEN title='STRING' THEN 1 ELSE 2 END
LIMIT 10
(SELECT * FROM movies WHERE title='STRING')
UNION
(SELECT * FROM movies WHERE title LIKE '%STRING%' ORDER BY x DESC LIMIT 10)

SQL query with IN clause

I am working on a page, thats like facebook feed page.
So, my query now look like that:
$sql = "SELECT *
FROM event
WHERE id_user IN (
SELECT a.id_user_stalkers
FROM stalkers a
WHERE a.id_user = ".$id_profile.")
ORDER by date DESC
LIMIT 0, 10";
stalkers - friends
Table Stalkers:
id id_user id_user_friend
I need to append to that IN clause my personal ID, so my qyery returns my events and my friends event.
Can anybody help me ?
What I have tried
$sql = "SELECT *
FROM event
WHERE id_user IN (
SELECT a.id_user_stalkers
FROM stalkers a
WHERE a.id_user = ".$id_profile.")
OR id_user = ".$id_profile."
ORDER by date DESC
LIMIT 0, 10";
it looks something wrong in your sql and your table
1-in your table you have id_user_friend and in your sql you are making id_user_stalkers.
2- i dont know if you have date in your first table , or you mean NOW() .? since u didnt share the first table.
try this
$sql = "SELECT * FROM event e WHERE id_user
IN (SELECT a.id_user_friend FROM stalkers a
WHERE a.id_user = ".$id_profile.")
OR e.id_user = ".$id_user."
ORDER by e.date DESC LIMIT 0, 10";
What is $id_user ? The original query only has one variable. I don't see why you need another. In other words, try this:
SELECT *
FROM event e
WHERE id_user IN (SELECT a.id_user_friend
FROM stalkers a
WHERE a.id_user = ".$id_profile."
) OR
e.id_user = ".$id_profile."
ORDER by e.date DESC LIMIT 0, 10

Categories