I have two tables, and I want to get the last enterd date.
The first table is seeker:
seeker_nic-----username
111-------------ali
222-------------umer
333-------------raza
The second one is requestblood:
id-------seeker_nic-----requireddate
1------- 111 ----------2012/10/9
2 ------- 222-----------2012/5/8
3 ------ 111-----------2012/12/12
4 ------- 111-----------2012/11/12
5---------111-----------2012/09/09
6 ------- 222-----------2012/7/9
7 ------- 333 ----------2012/4/4
Now, I want to list the users with their last inserted date like..
s.no---- username----- requireddate
1------- ali---------- 2012/09/09
2------- umer--------- 2012/7/9
3------- raza--------- 2012/4/4
i am using this query
SELECT seeker.username,bloodrequest.requireddate,
max( bloodrequest.bloodrequest_id ) AS maxdate
FROM seeker
JOIN bloodrequest ON seeker.seeker_nic = bloodrequest.seeker_nic
GROUP BY seeker.username
when i run this query in phpmyadmin.. its lists all user and show the first inserted date of each user.. but i want the last inserted date ... what should i do.. please i need help :(
EDIT for real answer:
Not very clean sql, but it will get you the results you are looking for. I'm sure there's a way do it better with group by, but this works. :)
SELECT s.username,
(
SELECT br1.requireddate
from bloodrequest as br1
where br1.bloodrequest_id =
(
select max(br2.bloodrequest_id)
from bloodrequest as br2
where br2.seeker_nic = s.seeker_nic
)
) as requireddate
FROM seeker as s
Related
I am trying to search two tables, match the results and then concatenate the answer... Only finding results >= today's date. This will then give the user the option to delete the selected from the DB. So...
Table 1 called Prog_name
id prog_name
1 Breakfast
2 Mid Morning
3 Afternoon
Table 2 called talk_ups
id date_tx prog_name (prog_name value = prog_name.id)
1 2017-06-30 2
2 2017-07-03 1
3 2017-07-01 3
The result I am after is something like: "01-07-2017, Afternoon". But I do also need the talk_ups.id to ensure it only deletes the correct record.
I managed to figure out how to get the name to match the talk_ups.prog_name value:
'$sql. = "SELECT talk_ups.prog_name, prog_name.id as progID, prog_name.prog_name as theName FROM prog_name, talk_ups WHERE talk_ups.prog_name = prog_name.id";'
But I can't figure out how to do the two searches and end up with the right result and how to separate out the results to then concatenate them.
You can use JOIN with WHERE condition, e.g.:
SELECT pn.id, pn.prog_name, tu.date_tx
FROM prog_name pn JOIN talk_ups tu ON pn.id = tu.prog_name
WHERE tu.date_tx > NOW();
I have a table which is having some report info. In that table I have userid and areaname .
Now my requirement is to get the count of user's list based on area
For example table will have
userid | areaname
-----------------
1 |area 1
1 |area 1
2 |area2
2 |area 2
2 |area2
3 |area1
3 |area1
4 |area3
5 |area2
---------------
Result must be
area1 2users
area2 2users
area3 1user
what is the mysql query to achieve this?
Use the Aggregate function COUNT() to get the number of users and GROUP BY to get the count based on areaname
Use DISTINCT for unique values
SELECT COUNT(DISTINCT userid),areaname FROM tablename GROUP BY areaname;
SELECT count(userid),area FROM YOURTABLENAME GROUP BY area;
I see, you can have dublicate entries ad well:
So I would make a sub Select
SELECT sum(partly_sum), area
FROM (
SELECT
userid, area , count(*) as partly_sum
FROM
_YOUR_TABLE_NAME_
WHERE 1
GROUP BY area, userid
) as a_bad_sub_query
WHERE 1
GROUP BY area
Regards
You can use distinct to avoid dupe entries
SELECT COUNT(DISTINCT userid),areaname FROM tablename GROUP BY areaname
I am generating a sales report and I am having a hard time with the query. I am not really good in query. I hope you can help me with this. Ok here it is.
I have a sales table. And the structure is like this
EX:
product_id name date_purchased
1 apple 2014-01-03 12:00:59
2 orange 2014-01-05 10:12:20
3 banana 2014-02-01 09:25:01
4 mango 2014-02-20 18:13:25
5 stawberry 2014-02-28 13:14:30
6 jackfruit 2014-05-26 08:16:31
7 grapes 2014-11-03 09:25:21
8 guava 2014-12-25 10:15:45
Now I want to count the item purchased every month
My output result should be like this
2,3,0,0,1,0,0,0,0,0,1,1
2 represents Jan,
3 represents Feb,
etc...
My query looks like this but I know it is wrong.. :(
SELECT COUNT(product_id) FROM sales_table GROUP BY date_purchased
How can I do that using a query? Or should I make a PHP function for this?
try like this :
SELECT year(date_purchased), month(date_purchased), COUNT(product_id)
FROM sales_table
GROUP BY concat(year(date_purchased), month(date_purchased))
I think you can retrieve the dates and use PHP as it is pretty straight forward.
Once you have a date lets say for example,
$date_str = "2014-01-03 12:00:59"; //let's say you fetch this from DB
$month = date('m',strtotime($date_str)); //this will automatically output 1
You mentioned, 2 represents Jan, 3 represents Feb etc, so you can just add 1 to $month and you have what you need.
Hope it should work
SELECT COUNT(product_id) FROM sales_table GROUP BY DATENAME(month, date_purchased)
The SQL query is enough.
You have to use group by.
But first make a month column for your table.
create function byMonth(#myDate datetime)
returns datetime
as
begin
declare #newDate
set #newDate = year(#myDate) + '/' + month(#myDate) + '/1 00:00:00'
return #newDate
end
go
and use this in your query:
Select Count(*),byMonth(date_puchased) as x
from myTable
where ...
group by x
having ...
this should work for you.
More elaborated solutions
To get Month - wise
SELECT COUNT(id) FROM testrec GROUP BY DATE_FORMAT(pur,'%M');
Get all details
SELECT COUNT(product_id),`name` , date_purchased, DATE_FORMAT(date_purchased,'%M') FROM sales_table GROUP BY DATE_FORMAT(pur,'%M');
I've tried everything to figure this out but I can't get the correct total. My attempts either add all the records and not just the latest ones or I only get the first record.
My first table: hubs
hubID hubName
1 hub1
2 hub2
My second table: hub_reports
reportID hubID date health school
1 1 2012-04-27 467 322
2 2 2012-04-23 267 22
3 1 2012-01-20 176 623
So what you see is 2 tables, one with the organizations name and other info and the second with the reports that each organization submits quarterly. I want to list all the organizations and their latest report. At the bottom of the table I want to add all the available health kits and school kits that are currently available.
Here's the code I'm using right now to display all the organizations and their latest reports.
SELECT * FROM (SELECT hubName, date, health, school FROM hub_reports,
hubs WHERE hub_reports.hubID = hubs.hubID ORDER BY date DESC) AS Total
GROUP BY hubName
This seems to work but when I try the same tactic to get the SUM of the health and school columns I don't get the right answer.
SELECT SUM(health) FROM (SELECT hubName, date, health FROM
hub_reports, hubs WHERE hub_reports.hubID = hubs.hubID ORDER BY date
DESC) AS Total GROUP BY hubName
I tried other using a LEFT JOIN approach that I found on another forum but it didn't seem to work any better. But I maybe I wasn't doing it right.
Please help!
I just encountered a similar problem in a project of mine. A variation of this query worked for me. Hope it is helpful to you.
SELECT hubs.hubName, hub_reports.*,
SUM(hub_reports.health) AS ttl_health,
SUM(hub_reports.school) AS ttl_school
FROM hubs, hub_reports
WHERE hub_reports.hubID = hubs.hubID
GROUP BY hub_reports.hubID
ORDER BY hub_reports.date DESC
Here's the PHP:
$rs = mysql_query( 'SELECT hubs.hubName, hub_reports.*,
SUM(hub_reports.health) AS ttl_health,
SUM(hub_reports.school) AS ttl_school
FROM hubs, hub_reports
WHERE hub_reports.hubID = hubs.hubID
GROUP BY hub_reports.hubID
ORDER BY hub_reports.date DESC' );
$grand_total['school']=0;
$grand_total['health']=0;
while ( $row = mysql_fetch_assoc( $rs ) ){ // Step through each hub
echo "{$row['hubName']} shows {$row['ttl_school']} total school, {$row['ttl_health']} total health";
$grand_total['school'] += $row['ttl_school'];
$grand_total['health'] += $row['ttl_health'];
}
echo "Grand Total School: {$grand_total['school']}, Grand Total Health: {$grand_total['health']}";
You're likely looking for the MAX() function.
Try this:
SELECT h.hubID, h.hubname, MAX(hr.date) as hrdate, SUM(hr.health) as health, SUM(hr.school) as school
FROM hubs h
LEFT JOIN hub_reports hr ON hr.hubID = h.hubID
GROUP BY h.hubID
Edit
You want the MAX date so it only returns the most recent entry (assuming your entries are entered by date, of course).
Here is the mySQL I got
id terms
1 a
2 c
3 a
4 b
5 b
6 a
7 a
8 b
9 b
10 b
I want to get an alphabetized list sorted by count as follows
terms count
a 4
b 5
c 1
What mySQL statement do I need for that?
I believe something like this will work:
SELECT terms, COUNT( id) AS count
FROM table
GROUP BY terms
ORDER BY terms DESC
Read : GROUP BY (Transact-SQL)
Groups a selected set of rows into a set of summary rows by the values of one or more columns or expressions in SQL. One row is returned for each group. Aggregate functions in the SELECT clause list provide information about each group instead of individual rows.
You just need to apply group by clause for getting result
select terms, count (id) as count from table
group by terms
order by terms
I had a very similar need for a used record store to display artists in stock alphabetically with their count in parenthesis e.g.:
Smokey Robinson and The Miracles (2) | Sonic Youth (2) | Spoon (3) | Steely Dan (1) | Stevie Wonder (2) | Sufjan Stevens (1) |
Note that I used SELECT DISTINCT when pulling from my table "records". Here are the relevant code snippets:
//QUERY
$arttool = mysql_query("SELECT DISTINCT * FROM records GROUP BY artist ORDER BY artist ASC");
//OUTPUT LOOP START
while($row = mysql_fetch_array($arttool)){
//CAPTURE ARTIST IN CURRENT LOOP POSITION
$current=$row['Artist'];
//CAPTURING THE NUMBER OF ALBUMS IN STOCK BY CURRENT ARTIST
$artcount = mysql_num_rows(mysql_query("SELECT * FROM records WHERE artist = '$current'"));
//ECHO OUT.
echo $current . "($artcount)";
The actual code in my site is more complicated, but this is the bare bones of it. Hope that helps...