these is my DB table data,{ i have maintained only one Table}
I need to fetch max 3 data from each start_date,
give me any idea to develop query,,
SELECT a.*
FROM Table1 a
INNER JOIN Table1 b ON a.start_date = b.start_date AND a.event_id <= b.event_id
GROUP BY a.event_id,a.start_date
HAVING COUNT(*) <= 3
ORDER BY a.start_date
I may suggest you this query -
SELECT * FROM (
SELECT t1.*, COUNT(*) pos FROM table t1
LEFT JOIN table t2
ON t2.start_date = t1.start_date AND t2.event_id <= t1.event_id
GROUP BY
t1.start_date AND t1.event_id
) t
WHERE
pos <= 3;
It selects 3 rows with min event_id in a start_date group.
Related
I am trying to do a MYSQL query where I get the most recent purchase for a user and then see if falls within certain criteria. Here is the query I put together:
select
users_purch.purch_date as purchase_date,
users_purch.total_amount as purchase_amount,
users.*
from
users
left join
(
select
max(date) as purch_date,
user_id,
total_amount
from
users_purchases
group by
user_id
) as users_purch
on users_purch.user_id = users.id
where
users_purch.purch_date < '2016-11-01'
and users_purch.total_cost < 112.49
order by
users_purch.purch_date desc
It seems that the query works but fails in certain aspects. For example, if a user has more than one purchase entry it is getting the max date but the amount as total_cost that the query retrieves is not from the same row as the max date. How can I rewrite this query to give me the most recent purchase record in its entirety?
Thanks!
You have to join once more to user_purchases table in order to get the information about the date:
select
users_purch.purch_date as purchase_date,
users_purch.total_amount as purchase_amount,
users.*
from
users
left join
(
select
max(date) as purch_date,
user_id
from
users_purchases
group by
user_id
) as users_purch
on users_purch.user_id = users.id
left join
(
select
user_id,
date,
total_amount
from
users_purchases
) as users_purch2 on users_purch.user_id = users_purch2.user_id and
users_purch2.date = users_purch.purch_date
where
users_purch.purch_date < '2016-11-01'
and users_purch.total_cost < 112.49
order by
users_purch.purch_date desc
due the group by you have in this select
if you need the amount of the max(date) rewrite extecting the proper amount
select
users_purch.purch_date as purchase_date,
users_purch.total_amount as purchase_amount,
users.*
from
users
left join
(
select t1.purch_date, t1.user_id, t2.total_amount from (
select
max(date) as purch_date,
user_id
from users_purchases
group by user_id ) t1
inner join (
date,
user_id,
total_amount
from users_purchases
) t2 on t1.user_id= t2.user_id, t1.purch_date = t2.date
) as users_purch
on users_purch.user_id = users.id
where
users_purch.purch_date < '2016-11-01'
and users_purch.total_cost < 112.49
order by
users_purch.purch_date desc
You can try this one, mate:
SELECT
up.date AS 'purchase_date',
up.total_amount AS 'purchase_amount',
u.*
FROM
users u
INNER JOIN users_purchases up ON up.user_id = u.user_id
INNER JOIN (
# get max date per user_id
SELECT user_id, max(date) AS 'purch_date'
FROM users_purchases
GROUP BY user_id
) max_up ON
max_up.user_id
# join that date to get the correct total_amount
AND max_up.`purch_date` = up.`date`
WHERE
up.`date` < '2016-11-01'
AND up.total_amount < 112.49
GROUP BY u.user_id
ORDER BY up.`date` DESC;
Note
Max Date per user_id
Value of total_amount per Max Date
Grouped per user_id
Ordered descending using users_purchases.date
I have two tables in db
1- i want to select distinct value from 2 tables
2- i want to print the number of each value
Ex: if i have on t1
t1
--------------
a
a
a
b
t2:
t2
--------------
a
b
c
the result will be:
a (4)
b (2)
c (1)
i try this but it not what i want
$sql=mysqli_query($conn,"select db_shopname from tbl_order UNION
SELECT db_shopname FROM tbl_item order by db_shopname asc")
or die(mysqli_error($conn));
$count=mysqli_num_rows($sql);
while($res=mysqli_fetch_array($sql)){
echo $res['db_shopname'];echo $count ;echo"<br/>";
}
You need UNION ALL instead of UNION. That way you merge the values from 2 tables into 1 virtual table. Then you can count the number of individual values using GROUP BY clause. Example:
select f, COUNT(f) as countf FROM
(select t1.f from t1 union all select t2.f from t2) t
GROUP BY f
SQL Fiddle
In PHP you can then use $res['countf'] to print the count
try this one :
select a.t1,count(a.t1) as cnt from(select t1 from t1 UNION all
select t2 as t1 from t2 )a group by a.t1
$query = "SELECT * FROM table3 WHERE name_id = '(SELECT name_id FROM table2
WHERE salary < 1000 && name = '(SELECT name FROM table1
WHERE savings > 1000)')'";
Basically I want to get the data from the table1 based on the savings and use it to get the data from table 2 and use that data to get all the information from table 3. But this wont work. Is my code right or am I doing something wrong?
I also cannot create new tables, I simply want to display the data from table 3.
Use join
SELECT * FROM table3 t3 join table2 t2
on t3.name_id=t2.name_id
join table1 t1
on t3.name=t1.name
where salary < 1000 and savings > 1000
$query="SELECT * FROM table3 LEFT JOIN table2 ON table3.name_id=table2.name_id
LEFT JOIN table1 ON table3.name=table1.name
WHERE table2.salary < 1000 AND table1.savings > 1000 "
Another syntax of join is
SELECT * FROM table1 t1,table2 t2 ,table3 t3
where t1.name = t3.name and
t2.name_id = t3.name_id and
t1.savings > 1000 and t2.salary < 1000;
$query = "SELECT t3.* FROM table3 t3
INNER JOIN table2 t2 ON t2.name_id = t3.name_id AND t2.salary < 1000
INNER JOIN table1 t1 ON t1.name = t2.name AND t1.savings > 1000";
I want to combine between two table. The output should be data from table A, table B.
SELECT date as Date, COUNT(*) as Transaction, SUM(status=0) as Success
FROM transfer_tx_201503 WHERE time >='00:00:00' AND time <= '$searchterm'
UNION SELECT date as Date, COUNT(*) as Transaction, SUM(status=0) as Success FROM request_tx_201503 WHERE time >='00:00:00' AND time <= '$searchterm' GROUP BY date desc"
i want out put like this: |2015-03-23 | 5 | 3 | 4 | 1 |
5 and 3 from table transfer_tx_2015, 4 and 1 from table request_tx_2015
Thank you
You can use table_a, table_b syntax:
SELECT columns FROM table_a, table_b WHERE table_a.id = table_b.id
Or you can use JOIN:
SELECT columns FROM table_a JOIN table_b ON table_a.id = table_b.id
You can read more about JOIN in:
https://dev.mysql.com/doc/refman/5.5/en/join.html
SELECT columns
FROM firstTable
JOIN secondTable ON
firstTable.columnName = secondTable.columnName
The common field is the date field, hence the join should be on that field.
Try using the following SQL:
SELECT t.date as Date, COUNT(*) as Transaction, SUM(t.status=0) as Success, COUNT(*) as Request, SUM(r.status=0) as RequestSuccess
FROM transfer_tx_201503 AS t,request_tx_201503 AS r WHERE t.time >='00:00:00' AND t.time <= '$searchterm' AND t.date=r.date
SELECT coloumn FROM Table_One, Table_Two WHERE Table_One.coloumnName = Table_One.coloumnName
I have drawn this image to explain what I need
1.to compare a user_id with the user_id's in two different tables
2.the corresponding ref_global_id from both the tables are then matched to a events table
3.matching global_id's from the events table are then arranged in ascending order.
Or this:
SELECT e.global_id, e.event_time
FROM (SELECT * FROM table1
UNION
SELECT * FROM table2) x inner join
event_table e ON e.global_id = x.ref_global_id
WHERE x.[user_id] = 121
SELECT e.global_id, e.event_time
FROM events_table e
JOIN table1 t1 on e.global_id = t1.ref_global_id
JOIN table2 t2 on e.global_id = t2.ref_global_id
WHERE t1.user_id = 121 AND t2.user_id = 121
ORDER BY e.event_time
Try this:
select global_id, event_time from event left join table1 on event.global_id = table1.ref_global_id AND table1.user_id = 121 left join table2 on event.global_id = table2.ref_global_id AND table2.user_id = 121