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
Related
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
I'm working with a system that perodically updates itself, within this system I try to select the highest 500 and list them. However, the problem is that I sort it by a varchar that contains a date in the format of 2013-07-08, this is a relic and for the time being I am annoyingly not allowed to change this to a proper date for easy sorting.
My question is, how can I select only 1 of the following 3 pretended results?
id| value | ownerid | date
1| 21300 | 1 | 2013-07-08
2| 21300 | 1 | 2013-07-08
3| 21300 | 1 | 2013-07-08
What I need done is to select one entry from each ownerid, which is the one with the highest value (if it's all the same it doesn't really matter which entry, just that it's only one!)
This is using a mysql database with PDO as the database layer.
Thankful for any and all help!
SELECT *
FROM [table]
ORDER BY STR_TO_DATE(yourdatefield, '%m/%d/%Y') DESC
LIMIT 5
Use a sub-query with a GROUP BY to get a particular id for each day (max or min for example) then JOIN that against the table to get the rest of the details:
SELECT a.id, a.value, a.ownerid, a.date
FROM SomeTable a
INNER JOIN
(
SELECT date, MAX(id) AS MaxId
FROM SomeTable
GROUP BY date
) b
ON a.date = b.date
AND a.id = b.MaxId
Modified version to get the highest value row for each day. As value isn't unique I have done a further sub select to get the highest id for that highest value. Without this you could get multiple rows returned for a day. If there is zero chance of the other values every being different you could use DISTINCT to remove the multiples, but normally not possible to be certain of that.
SELECT a.id, a.`value`, a.ownerid, a.`date`
FROM SomeTable a
INNER JOIN
(
SELECT `date`, MAX(`value`) AS MaxVal
FROM SomeTable
GROUP BY `date`
) b
ON a.`date` = b.`date`
AND a.`value` = b.MaxVal
INNER JOIN
(
SELECT `date`, `value`, MAX(id) AS MaxId
FROM SomeTable
GROUP BY `date`, `value`
) c
ON b.`date` = c.`date`
AND b.MaxVal = c.`value`
AND a.id = c.MaxId
I have table
**id name status date**
1 john 2 01.01.2010
2 mike 5 04.01.2010
3 john 2 06.01.2010
4 sam 1 08.01.2010
john has status 2 twice and i need to select john,mike from this table where status = 2 but i need to show latest record.
I cannot use order by i use it already for something else.
You can use order by for multiple criteria like this:
ORDER BY date desc, status desc
You need to use a correlated subquery such as this:
select *
from table t1
where t1.date = ( select max( t2.date )
from table t2
where t1.name = t2.name
and t1.status = t2.status )
The query would go much faster if you didn't need the ID field:
SELECT t.name, t.status, max(t.date) date
FROM table t
GROUP BY t.name, t.status
ORDER BY [whatever]
If you DID need id, AND the ID is guarenteed to be larger on the record with the newer date, you could just add max(t.id) id to the field list.
SELECT *
FROM table t
WHERE status = 2
AND date = (SELECT MAX(date) FROM table tmp WHERE tmp.name = t.name GROUP BY name)
I have a table full of entries with DATETIME time stamps, and an ID field. I want to make a MySQL query (in my PHP script) that selects from another table with the matching ID. I want to make some kind of join that will sort by the number of entries in the first table with a timestamp more recent than 24 hours. So basically, if there are 30 entries in the first table with a timestamp of less than 24 hours with the ID "334" then I want to select the row from the second table with the ID of 334. And that should come before an entry with the ID "234" that only has 20 entries within the last 24 hours.
I hope this is clear... I'm really stumped on how to do this, so thanks for any help. :D
Use:
SELECT a.*,
x.num
FROM TABLE_A a
JOIN (SELECT t.id,
COUNT(*) AS num
FROM TABLE_B t
WHERE t.timestamp BETWEEN DATE_SUB(NOW, INTERVAL 1 DAY)
AND NOW()
GROUP BY t.id) x ON x.id = a.id
ORDER BY x.num DESC
Try:
Select b.ColA, b.ColB, b.ColC,-- etc.
Count(*) count
From TableB b
Join TableA a
On a.Id = b.Id
And a.TimeStamp > getDate() - 1
Group By b.ColA, b.ColB, b.ColC -- etc.
Order By Count(*) Desc
If you also want to see the rows from TableB that have no timestamps in the past 24 hours then use an outer join:
Select b.ColA, b.ColB, b.ColC,-- etc.
Count(*) count
From TableB b
Left Join TableA a
On a.Id = b.Id
And a.TimeStamp > getDate() - 1
Group By b.ColA, b.ColB, b.ColC -- etc.
Order By Count(*) Desc
Two tables:
table_a
-------
table_a_id: (primary, int)
table_b
-------
table_a_id: (index, int, from table_a)
table_b_value: (varchar)
A one to many relationship between table_a_id and table_b_value.
Given a query like this:
SELECT DISTINCT(table_a_id) FROM table_a
JOIN table_b ON table_a.table_a_id=table_b.table_a_id
I want to order by the number of occurrences of table_a_id in table_b. I'm not really sure how to write this in MySQL.
SELECT COUNT(table_b.table_a_id) AS count
FROM table_a
JOIN table_b ON table_b.table_a_id = table_a.id
GROUP BY table_b.table_a_id
ORDER_BY count DESC
SELECT
a.table_a_id,
COUNT(b.table_a_id) AS a_count
FROM table_a AS a
LEFT JOIN table_b AS b
ON a.table_a_id = b.table_a_id
GROUP BY a.table_a_id
ORDER BY a_count
Why do you need to join the tables at all in this case?
SELECT table_a_id FROM table_b GROUP BY table_a_id order by count(table_a_id) desc
Using your example
SELECT table_a_id, COUNT(*)
FROM table_b
GROUP BY table_a_id
A real world example
If you want a table like:
post_id comments_count
1 20
2 125
3 43
If you might need other information from the A table, you can:
select p.id, count(c.*) comments_count
from posts p
join comments c on c.post_id = p.id
group by p.id
If you don't need anything from the A table, you can simplify:
select post_id, count(*)
from comments
group by post_id