php mysql Join 2 tables without common values - php

I have 2 tabes with the following fields
Table1::
Id, Name, startDate, File, Current test
Data set:
1 nm1-tbl1 25-10-2013 file1 yes 1
1 nm2-tbl1 27-10-2013 file2 yes 1
Table2::
Id, Name, startDate, File, Enddate
Data
1 nm1-tbl2 24-10-2013 file1 11-11-2014
1 nm2-tbl3 26-10-2013 file2 11-11-2014
I need the out put as
1 nm1-tbl2 24-10-2013 file1
1 nm1-tbl1 25-10-2013 file1
1 nm2-tbl3 26-10-2013 file2
1 nm2-tbl1 27-10-2013 file2
Both tables have no common values. But I need to combile these 2 tables in order by the ASC OR DESC
select a.*, b.*
from table1 as a, table2 as b
where a.File <> '' AND b.File <> '' AND a.startDate <> '0000-00-00'
AND b.startDate <> '0000-00-00' order by a.startDate ASC, b.startDate ASC
But it is not working as expected. It first orders the table1 and then table2. But I need as combination of 2. How to achieve this. Please help me.

(
select
Id,
Name,
startDate,
File
from
table1
)
union
(
select
Id,
Name,
startDate,
File
from
table2
)
order by
startDate DESC;

I think you need to use UNION here:
(SELECT * FROM Table1)
UNION
(SELECT * FROM Table2)
ORDER BY startDate DESC;
UNION is used to combine the result from multiple SELECT statements into a single result set.
http://dev.mysql.com/doc/refman/5.0/en/union.html

Use union query to get this result
SELECT Id,Name,startDate,File
FROM table1
UNION
SELECT Id,Name,startDate,File
FROM table2
ORDER BY startDate ASC

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...

Limit each element in "IN" operator Mysql [duplicate]

I am struggling to find an optimal solution for the following problem.
Suppose I have a table 'Table' like this:
id name report_id
1 name1 1
2 name2 3
3 name3 5
4 name1 7
5 name3 8
....................
I want to select for each value in a set: ('name1', 'name2') 10 random unique rows.
Of course it is possible to do with union like:
(SELECT * FROM Table
WHERE
name='name1'
ORDER BY RAND() LIMIT 10)
UNION
(SELECT * FROM Table
WHERE
name='name2'
ORDER BY RAND() LIMIT 10)
But if I have 100 unique names for which I have to select 10 random records - this query is going to be a bit large.
SQLFiddle demo
select ID,NAME,REPORT_ID
from
(
select *, #row:=if(name=#name,#row,0)+1 as rn, #name:=name from
(select *,RAND() as trand from t) t1,
(select #row:=0,#name:='') tm2
order by name,trand
) t2
where rn<=10
Try this:
SELECT
id,
name,
report_id
FROM
(
SELECT id,
report_id,
name,
CASE WHEN #name != name THEN #rn := 1 ELSE #rn := #rn + 1 END rn,
#name:=name
FROM (SELECT * FROM tbl ORDER BY RAND()) a,
(SELECT #rn:=0, #name := NULL) r
ORDER BY name
) s
WHERE rn <= 10;
SQL FIDDLE DEMO
This doesn't work in MySQL, but in PostgreSQL you can use partition by
select name,report_id from
(select name,report_id,row_number()
over
(partition by name order by random())
as rn from Table) a
where rn<=10
I had this same question and found this answer from a colleague.

how to find the most repeated values in sql

In my table i have something like
id file_id
1 1
1 2
1 4
2 1
2 4
2 6
I need to find the most frequent file_id regardless of the id, so for example i need the output something like
file_id count
1 2
4 2
.. ..
Im using php and phpmyadmin
Use this query
select
file_id,
count(*) as count
from table_name
group by file_id;
select file_id, count(*) as count
from files
group by file_id
SELECT file_id, count(id)
FROM f
GROUP BY file_id
ORDER BY count(file_id) DESC;
In standard SQL, you can do:
select f.file_id, cnt
from (select file_id, count(*) as cnt,
rank() over (order by count(*) desc) as seqnum
from t
group by file_id, id
) f
where seqnum = 1;
Simple SQL in your case:
select file_id, count(id)
from ur_table
group by file_id;
Using aggregate function
SELECT count(id) AS count FROM table GROUP BY file_id

SQL: How can I find repeated value's till separation

What I want to do with SQL is find repeating values in a column until there's an other value sorted by date.
This is my table. I use "CURRENT_TIMESTAMP" for the date column.
Name |date
-------------------
Bart |12-12-2014
Bart |23-12-2014
Joost |24-12-2014
Bart |25-12-2014
Bart |26-12-2014
Bart |27-12-2014
So in this example I want the number "3" returned and the last known value of name, so in this case "Bart".
I hope I made myself clear, sorry for the unclear title!
Hmmm . . . Here is one method where the logic is placed in the where clause:
select count(*), max(lastname.name)
from tablename t cross join
(select t2.name from tablename t2 order by date desc limit 1) as lastname
where t.name = lastname.name and
t.date > (select max(t2.date) from tablename t2 where t2.name <> lastname.name);
You can use the following query:
SELECT Name, COUNT(*) AS cnt
FROM (
SELECT Name, [date],
ROW_NUMBER() OVER (ORDER BY [date]) -
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY [date]) AS grp
FROM mytable ) AS t
GROUP BY Name, grp
ORDER BY COUNT(*) DESC
This query tries to identify islands of records, i.e. consecutive rows having the same Name: grp calculated field does exactly this.
If you want the Name having the largest number of consecutive records, just use TOP 1 in the above query.
Demo here

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)

Categories