MySQL select first five unique rows depending on two columns - php

I have a mysql table which have two columns for selecting propouses - tablica and contentid as it shows here
i want to select first five rows sorted by date DESC which have unique combination of both columns - tablica and contentid
how that can happend? I've tried already with combinations of distinct, group_concat and concat but everything i've tried skip some of the rows.
please help.

Hello_ mate
If you don't need other stuff than those two fields you can use the query I did post in the comments below your question.
Otherwise if you need all the fields you can use this query:
SELECT `id`, `tablica`, `contentid`, `ip`, `userid`, MAX(date) as `date`
FROM test2
GROUP BY tablica, contentid
ORDER BY date DESC
LIMIT 5;
using the MAX function we select the latest matching date for this tablica and contentid, if you want to get oldest date use MIN function
Let me know if this works for you.
Good Luck!

Related

Select from multiple tables using union all and single order by ,php

I ran into an error I cant seem to come out of.
am not too good with unions
I want to loop through 4 different tables(using union all) and manipulate their values to fit my needs.
I also need to use single 'ORDER by Date DESC' (Date are integer values) for the whole union all, so that I can arrange the output in a pattern,
when I add the 'order by date desc ' to it, code doesn't work . and when I remove it , the values of the second query are attached to the names of the first query, am sooo confused.
I tried "Select * from table_name where..... it idnt work in this case , that's why I had to bring out all table_names I need to the query,
Basically , I want to echo each value from the query uniquely when I need to,
any help is appreciated, thanks
<?php
$q114="(SELECT id AS id1,text_post AS text_post1,likes AS likes1
FROM timeline_posts WHERE email='$owner_email')
UNION ALL (SELECT pic_comment AS pic_comment2, comments AS comments2, date AS date2
FROM pictures WHERE email='$owner_email')
UNION ALL (SELECT image AS image3,likes AS likes3, comments AS comments3
FROM profile_pics WHERE email='$owner_email')
UNION ALL (SELECT likes AS likes4, comments AS comments4, date AS date4
FROM friends_timeline_post WHERE timeline_email='$owner_email')
ORDER BY 'date' DESC";
$pages_query=mysqli_query($connect,$q114);
while($fetch9=mysqli_fetch_assoc($pages_query))
{
print_r($fetch9['likes3'] );
//a lot of work to be done here
}
?>
For "unioning" the results of multiple selects and ordering those results by a column name across all the results of the multiple selects, column names must be the same in all selects.
You could add a column to all selects that would contain your "digit" in order to still be able to distinguish between let say "likes1" and "likes2" even if their column name is "likes" for all the selects that you "unioned".

Selecting multiple tables and displaying them ordered by datetime column

I want to display the logs to recent activities page ordered by date. Now I was trying to execute this to my mysql
"SELECT * FROM tracking_log.editlog, tracking_log.deletelog, tracking_log.loginlog, tracking_log.logoutlog ORDER BY time ASC";
but it always says
Column 'time' in order clause is ambiguous
all of the tables have a time column, format by datetime (0000-00-00 00:00:00)
How am I going to fetch them ordered by time?
Thanks in advance!
By which table's time column you want to order?
Assuming you want to order the result set by tracking_log.editlog.time column then the query would look like below:
SELECT
*
FROM tracking_log.editlog, tracking_log.deletelog,
tracking_log.loginlog, tracking_log.logoutlog
ORDER BY tracking_log.editlog.time ASC;
Just in case if all of the time columns in the respective table don't contain NOT NULL values at the same time then you need to use COALESCE I guess.
Query using COALESCE
SELECT
*
FROM tracking_log.editlog, tracking_log.deletelog,
tracking_log.loginlog, tracking_log.logoutlog
ORDER BY
COALESCE(tracking_log.editlog.time , tracking_log.deletelog.time, tracking_log.loginlog.time,tracking_log.logoutlog.time) ASC;
'tracking_log' is your database name, and you're selecting multiple tables from that database, so you need to specify from which table you want to order 'time' by:
select * from tracking_log.editlog, tracking_log.deletelog ORDER BY tracking_log.editlog.time ASC
or whichever table from your database you want to use 'time' from. This will fix the error but won't return any results because you have multiple tables in a SELECT clause without anything relating them together.
You'll need to specify some common columns on which you want to return results rather than getting the wildcard and then UNION the tables to aggregate the results. For example, if you have common columns userID, description and time in all your tables, you could do the following:
(SELECT userID, description, time FROM tracking_log.editlog)
UNION
(SELECT userID, description, time FROM tracking_log.deletelog)
ORDER BY time

Select Duplicate Sub String of Records to Single Record Using Group By

Is it possible writing SQL to select duplicate sub string of records from a table into single records ? I just want to group the month and year so the result look like this picture:
I try this, but it didn't work.
SELECT DATE, SUBSTRING(DATE, 3, 6) as Addrow FROM dbo.n4abs_premi_olah GROUP BY Addrow
Any advice will be appreciated. Thank you !
SELECT DISTINCT(DATE_FORMAT(date, '%m/%Y')) month
FROM Table
If you actually need to use GROUP BY (because you're also selecting other aggregates), do:
SELECT DATE_FORMAT(date, '%m/%Y')) month, other stuff...
FROM Table
GROUP BY month
Here's one of many ways to do it:
SELECT DISTINCT CONCAT(MONTH(date), '/', YEAR(date)) FROM table;

Get unique values of SQL table and echo with PHP

I have a MySQL table with over 200 values. One of the columns on my table is 'date'. Out of all 200 values there are only 5 unique dates.
How can I list out the unique values of the dates and echo them with php. e.g. not getting back 200 instances of dates but just 5.
Use DISTINCT
SELECT DISTINCT `date` FROM `tablename`....
SELECT myDate FROM myTable GROUP BY myDate
Or...
SELECT DISTINCT myDate FROM myTable
DISTINCT is a nice short hand, but if you ever then want to make use of the query for other purposes, if often constrains you a bit too much. So I prefer the GROUP BY version.

MySQL Distinct (Unique) values over time

I have a question about constructing a MySQL query. I have a table with one column containing values, and another column containing timestamps. What I'd like to do is get the number of distinct (unique) values for a field from a specific epoch up until various points in time so that I can plot the number of unique values over time. For example, I'd like the query result to look like the following:
Date, COUNT( DISTINCT col1)
2011-02-01, 10
2011-02-02, 16
2011-02-03, 24
etc.
Note that these values are the number of distinct values starting the same point in time. Currently to accomplish this, I'm using a loop in PHP to iterate a single query for each date and it takes forever since I have a large DB. To give a better picture, the inefficient code I'd like to replace looks like the following:
for($i=0;$i<count($dates),$i++){
$qry = "SELECT COUNT (DISTINCT `col1`) FROM `db`.`table` WHERE `Date` BETWEEN '".$EPOCH."' AND '".$dates[$i]."';";
}
Any help would be appreciated.
Thanks
If understood your question, you can use a GROUP statement:
SELECT StampCol, COUNT(DISTINCT DataCol) FROM MyTable GROUP BY StampCol
SELECT DATE_FORMAT(date_column, "%Y-%m-%d") AS date_column, COUNT(visitors) AS visitors FROM table GROUP BY DATE_FORMAT(date_column, "%Y-%m-%d") ORDER BY date_column desc"

Categories