fetch column2 value corresponding to column1 maximum,minimum? - php

Is it possible to fetch column2 values for max(column1). help me. I got to find the date corresponding to the max(price) and min(price) in database.
$str="select MAX(psq_price),MIN(psq_price),AVG(psq_price) from crawl_archives where p_id=2570";
I found max , min values from this query.. Need to find the date column corresponding to the maximum price ..

try this
select date , MAX(psq_price),MIN(psq_price)
from mytable
group by date

try something like this:
select *
from <table> t
join (select max(price) as max_price
from <table>
group by <col1>
)a
on a.col1=t.col1
and a.max_price=t.price

Related

phymyadmin add row number in query result

Hi i have this sql code i want to query in phpmyadmin
SELECT DISTINCT `unit`,`location` FROM `myasset` ORDER BY `unit` asc
It is possible to add custom number in my sql result
I tried something like below..its not working ..i received message row_number function does not exist
SELECT DISTINCT Row_Number(),`unit`,`location` FROM `myasset` ORDER BY `unit`
SELECT Row_Number() DISTINCT`unit`,`location` FROM `myasset` ORDER BY `unit`
First you have to set a value like
SET #rnum = 0;
then
SELECT #rnum:=#rnum+1 AS row_num, DISTINCT unit,location FROM myasset ORDER BY unit asc
Hope it will solve your issue.
Thanks
You could do
select #rownum:=#rownum+1 No,DISTINCT unit,location FROM myasset ORDER BY unit, (SELECT #rownum:=0);

query to Ignore the repeated column in leaderboard

i have a problem my database looks like this DATABASE STRUCTURE
i want to sort the lboard and ignore the repeated values
like this REQUIRED OUTPUT
can anyone help me?
You could use GROUP BY to get single result for one user and MAX function to get its maximum score
the query will be like this
SELECT id, exam_id, user_id, MAX(lboard) FROM `leaderb` GROUP BY user_id ORDER BY lboard DESC
SELECT id, exam_id, user_id, MAX( lboard )
FROM wp_watu_takings WHERE exam_id = 3
GROUP BY user_id
ORDER BY MAX( lboard ) DESC

Distinct Combinations According to Last Date (mySQL/PHP)

I have a table with four columns:
id
date_added
variable1 (eg. a,b,c,d,e,f)
variable2 (eg. 1,2,3,4,5)
What I want is to echo out every distinct combination of variable1 and variable2
However, there will be multiple dates (date_added) which will have the same combination. I only want to echo out the last combination (according to date). The rows that are echoed out, may have different dates.
Is there any way that I can do that?
Try to use this:
SELECT variable1,
variable2
FROM
(SELECT *
FROM Table1
ORDER BY date_added DESC) tmp
GROUP BY variable1,
variable2;
Try this
SELECT distinct(variable1) variable1, (select distinct (variable2)) as variable2
FROM bmi_users
Order by date_added DESC
i hope this will work

How do i count and sum the values from the database

I have a table in mysql which holds the values in integers, my table is something like this.
I want to sum up the values of a particular column, for example i want to calculate the total amount, total cashpaid and total balance. hwo do i do it?
Use SUM() function of MySQL like this:
select SUM(amount) from tablename;
select SUM(cashpaid) from tablename;
select SUM(balance) from tablename;
OR you can group them into one:
select SUM(amount), SUM(cashpaid), SUM(balance) from tablename;
if you want to do it in one single query:
SELECT SUM(amount) as total_amount, SUM(cashpaid) as total_paid,SUM(balance) as total_balance FROM tablename;
to count element use COUNT()
SELECT COUNT(*) FROM tablename;
it's better to use aliases for the column names when using this functions.
Try out this:
select SUM(amount) AS total_amount, SUM(cashpaid) AS total_cashpaid, SUM(balance) AS total_balance from tablename;
try
select sum(amount), sum(cashpaid), sum(balance) from tablename
For counting the total number of records use count() function.
like:
select count(amount) from table_name;
It will return from above table in your question 3.
For Sum Use SUM() in SELECT query.
Like:
select SUM(amount) as total_amount,SUM(cashpaid) as total_cash_paid,SUM(balance) as total_balance from table_name;
after as is your new column name which will automaticaly create after executing of query.

php, sql selection

I have a stupid question, I have this table :
id_product name value
1 price 10-20
1 type computer
2 price 20-30
3 price 100-200
and I want to select from this table GROUP BY id_product and ORDER BY value WHERE name='price'
how can i do this?
Thanks a lot
select * from table_name WHERE name='price'
GROUP BY id_product
ORDER BY value
In PHP
mysql_query("select * from table_name WHERE name='price'
GROUP BY id_product
ORDER BY value") or die(mysql_error());
SELECT *
FROM table_name
WHERE name = 'price'
GROUP BY id_product
ORDER BY value
You can see this for MySQL Select syntax
select * from table_name where name='price' Group By id_product ORDER By value;
right off the top of my head...
does it work?
SELECT * FROM `table` WHERE `table`.`name`='price' GROUP BY `table`.id_product ORDER BY `table`.`value` ASC
It depends on how you want to group items with the same id: which one do you want to be shown? In theory GROUP BY shouldn't be used with SELECT * (it does not work with non mysql databases) and should be associated with aggregate funcions such as COUNT(), MAX(), etc.
You might just need SELECT DISTINCT instead of GROUP BY.
This will select distinct id_product from table_name and order by value where name is "price". I think this will give the outcome you're after.
SELECT DISTINCT id_product
FROM table_name
ORDER BY (
SELECT t.value FROM table_name t
WHERE t.name='price'
AND t.id_product = table_name.id_product
)

Categories