mysql join to list item from two tables - php

I am having two tables table_purchase1 and table_purchase2. From these tables i want to list purchase history with order by date.
My tables
table_purchase1
purchase_id user_id item date
1 1 AA1 2011-06-10
2 1 AA2 2011-06-12
table_purchase2
purchase_id user_id item date
1 1 BB1 2011-06-11
2 1 BB2 2011-06-13
Expecting result
item
BB2,AA2,BB1,AA1
How can i do this
Thanks

You can use UNION:
(SELECT item, `date` FROM table_purchase1)
UNION
(SELECT item, `date` FROM table_purchase2)
ORDER BY `date`
If you need single row following might work:
SELECT GROUP_CONCAT(
( (SELECT item, `date` FROM table_purchase1)
UNION
(SELECT item, `date` FROM table_purchase2)
ORDER BY `date`
))

(SELECT
item
FROM
table_purchase1)
UNION
(SELECT
item
FROM
table_purchase2)
ORDER BY `date`

$sql = (SELECT item, date FROM table_purchase1) UNION (SELECT item, date FROM table_purchase2) ORDER BY date

Related

multiple sum from different tables [duplicate]

I have a union of three tables (t1, t2, t3).
Each rerun exactly the same number of records, first column is id, second amount:
1 10
2 20
3 20
1 30
2 30
3 10
1 20
2 40
3 50
Is there a simple way in SQL to sum it up, i.e. to only get:
1 60
2 80
3 80
select id, sum(amount) from (
select id,amount from table_1 union all
select id,amount from table_2 union all
select id,amount from table_3
) x group by id
SELECT id, SUM(amount) FROM
(
SELECT id, SUM(amount) AS `amount` FROM t1 GROUP BY id
UNION ALL
SELECT id, SUM(amount) AS `amount` FROM t2 GROUP BY id
) `x`
GROUP BY `id`
I groupped each table and unioned because i think it might be faster, but you should try both solutions.
Subquery:
SELECT id, SUM(amount)
FROM ( SELECT * FROM t1
UNION ALL SELECT * FROM t2
UNION ALL SELECT * FROM t3
)
GROUP BY id
Not sure if MySQL uses common table expression but I would do this in postgres:
WITH total AS(
SELECT id,amount AS amount FROM table_1 UNION ALL
SELECT id,amount AS amount FROM table_2 UNION ALL
SELECT id,amount AS amount FROM table_3
)
SELECT id, sum(amount)
FROM total
I think that should do the trick as well.
As it's not very clear from previous answers, remember to give aliases (on MySQL/MariaDb) or you'll get error:
Every derived table must have its own alias
select id, sum(amount) from (
select id,amount from table_1 union all
select id,amount from table_2 union all
select id,amount from table_3
) AS 'aliasWhichIsNeeded'
group by id
Yes!!! Its okay! Thanks!!!!
My code finishing:
SELECT SUM(total)
FROM (
(SELECT 1 as id, SUM(e.valor) AS total FROM entrada AS e)
UNION
(SELECT 1 as id, SUM(d.valor) AS total FROM despesa AS d)
UNION
(SELECT 1 as id, SUM(r.valor) AS total FROM recibo AS r WHERE r.status = 'Pago')
) x group by id
SELECT BANKEMPNAME, workStation, SUM (CALCULATEDAMOUNT) FROM(
SELECT BANKEMPNAME, workStation, SUM(CALCULATEDAMOUNT) AS CALCULATEDAMOUNT,SALARYMONTH
FROM dbo.vw_salaryStatement
WHERE (ITEMCODE LIKE 'A%')
GROUP BY BANKEMPNAME,workStation, SALARYMONTH
union all
SELECT BANKEMPNAME, workStation, SUM(CALCULATEDAMOUNT) AS CALCULATEDAMOUNT,SALARYMONTH
FROM dbo.vw_salaryStatement
WHERE (ITEMCODE NOT LIKE 'A%')
GROUP BY BANKEMPNAME, workStation, SALARYMONTH) as t1
WHERE SALARYMONTH BETWEEN '20220101' AND '20220131'
group by BANKEMPNAME, workStation
order by BANKEMPNAME asc
IN MSSQL You can write this way, But Doing UNION ALL THE Column should be the same for both ways.
I have given this example So that you can understand the process...

How to order united request by date?

I select 'title' from one table and order by 'date'. I select 'title' from other table and order by 'date'(two columns with same name in two tables). Then I unite them like this.
(SELECT title FROM books WHERE id = :id ORDER BY date DESC) UNION ALL (SELECT title FROM movies WHERE id = :id ORDER BY date DESC);
How can I oder them all together by date?
If you do not select date, you can't; if you add date to the selects, you can just remove the parenthesis, and the first ORDER BY clause. Without parenthesis, MySQL applies ORDER BY to the entire UNION
For ordering a column you must have in select like the sample below
SELECT title, `date` FROM books WHERE id = :id
UNION ALL
SELECT title, `date` FROM movies WHERE id = :id
ORDER BY date DESC
You can sort using order by only columns present in select ..so you need date .. and you can only union table with the same column ( number, type) so the date is in both the select .. and in union the order is needed only at the end of the query

Order by distinct number of rows for 3 tables?

I have 3 tables - News, Content and Edits.
Every row in each table contains a user_id field.
What I want to do is make a list of the top contributors to these 3 tables.
So I need to count how many times each different user_id appears in each table and order from the highest count to the lowest.
Like this:
SELECT
user_id,
COUNT(*) AS all_actions
FROM (
SELECT user_id FROM News
UNION ALL
SELECT user_id FROM Content
UNION ALL
SELECT user_id FROM Edits
) tmp_table
GROUP BY user_id
ORDER BY all_actions DESC
This should do it.
SELECT all_user_ids.user_id as `user_id`, COUNT(all_user_ids.user_id) as `count`
FROM
(SELECT user_id FROM news
UNION ALL
SELECT user_id FROM content
UNION ALL
SELECT user_id FROM edits) AS all_user_ids
GROUP BY `user_id`
ORDER BY `count` DESC
This would be a per table instance
SELECT DISTINCT(COUNT(user_id)) as num FROM news GROUP BY user_id ORDER BY num DESC LIMIT 0,3

Select latest record if column has 2 of the same

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)

Mysql Query: switch if record come from table news or news_collect

I have this query:
(SELECT * FROM news WHERE Del='0' ORDER BY `Date` DESC)
UNION
(SELECT * FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)
Now in the while loop I need to switch if record come from table news or news_collect...How do it??
I would solve this problem adding an custom field, keeping the column count the same.
(SELECT *, 'news' as source FROM news WHERE Del='0' ORDER BY `Date` DESC)
UNION
(SELECT *, 'news_collect' as source FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)
Then you can use switch($aRow['source']).
Try modifying your query as follows:
(SELECT news.*, 'news' coming_from FROM news WHERE Del='0' ORDER BY `Date` DESC)
UNION
(SELECT news_collect.*, 'new_collect' FROM news_collect WHERE Del='0' ORDER BY `Date` DESC)
(assuming "coming_from" is not a column of the "news" table)
Then use column "coming_from" to distinguish between the record's origin.

Categories