Select and group MySql table - php

I need a little help to count and group some MySql table records according of a range of dates given.
The table strcutre is this:
In advance, thank you for your help

This shoud give you the result that you need:
SELECT
d1.dt,
COUNT(DISTINCT t1.recid) AS departures,
COUNT(DISTINCT t2.recid) AS returns,
COUNT(DISTINCT t1.recid) + COUNT(DISTINCT t2.recid) AS total
FROM (SELECT departure_date AS dt FROM yourtable
UNION SELECT return_date FROM yourtable) d1
LEFT JOIN yourtable t1 ON d1.dt = t1.departure_date
LEFT JOIN yourtable t2 ON d1.dt = t2.return_date
GROUP BY
d1.dt
The first subquery will return all dates, not duplicated, present in your table (departures and returns).
I'm then trying to join each date to a departure date, using a LEFT JOIN. I'm then counting the DISTINCT t1.recid that make the join succeed..
I'm then trying to join each date to a return date, using LEFT JOIN. Total is the sum of both counts.
Fiddle is here.

Related

Writing complex SQL query with 4 tables

I have 4 tables from which i want to output information with a single query and i'm not sure how to do that.
• From table1 i want to get all the records
• For each record from table1 i want to take out the SUM from field1 from all records in table2 on a matching id taken from table1
• For each record from table1 i want to take out the SUM from field1 from all records in table3 on a matching id taken from table1
• For each record from table1 i want to take out the value of a single record from table4 on a matching id taken from table1
EDIT:
Here is how i think the graphic for my request should look:
Here's my working code:
SELECT DISTINCT
i.id,
i.dateCreated,
i.dateBilled,
i.dateCompleted,
i.userId,
i.type,
i.status,
i.truck,
i.poNumber,
i.total,
i.billtoId,
i.shiptoId,
i.invoiceNumber,
i.loadNumber,
SUM(p.amount) as amountpaid,
c.name as billtoName
FROM `invoices` as i
LEFT JOIN `invoice_payments` as p ON i.id = p.invoice
RIGHT JOIN `companies` as c ON c.id = i.billtoId
GROUP BY i.id, i.invoiceNumber
You can see how i managed to get the SUM from all payments on my invoices with a left join. I'm trying to do the same for i.total, but as soon as i add another LEFT JOIN my calculations come up wrong and the result in amountpaid doubles
You can write this query with subqueries in the SELECT statement:
SELECT id,
(SELECT sum(field1) FROM t2 WHERE t2.idfrom1=t1.id) AS firstSum,
(SELECT sum(field1) FROM t3 WHERE t3.idfrom1=t1.id) AS secondSum,
(SELECT min(field1) FROM t4 WHERE t4.id=t1.f2 LIMIT 1) AS singleRecord
FROM t1
This is the idea, you just have to adapt it to your schema.
edit: updated from the drawing

how to display data between two specific dates with date format as timestamp

I am trying to display data from table between two specific dates, date format is timestamp.
This query is working fine without dates.
SELECT table1.username,COUNT(table2.userid) as total FROM table2 INNER JOIN table1
ON table1.userid = table2.userid GROUP BY table1.username
ORDER BY COUNT(table2.userid) DESC
I am using below query to get data between two specific dates. but it does not display anything
SELECT table1.username,COUNT(table2.userid) as total FROM table2
WHERE table2.date between '2015-01-0' and '2015-01-30' INNER JOIN table1 ON
table1.userid = table2.userid GROUP BY table1.username
ORDER BY COUNT(table2.userid) DESC
Try
SELECT table1.username,COUNT(table2.userid) as total
FROM table2
INNER JOIN table1 ON table1.userid = table2.userid
WHERE table2.date between '2015-01-0' and '2015-01-30'
GROUP BY table1.username
ORDER BY COUNT(table2.userid) DESC
I Guess we cannot use where clause before writing join.

MYSQL count subquery on current row

I am having trouble with this query and am hoping someone can help.
SELECT
myTable.id,
myTable.subject,
myTable.upvotes,
myTable.downvotes,
(SELECT COUNT(id)
FROM myTable
WHERE myTable.thread = myTable.id) AS comments_count
FROM myTable
Basically I have a table with posts and comments, the comments thread is tied to the id of the original post. In my query I want to show how many relpies (how many threads = id) from all rows for the current id/row.
I hope that makes sense :)
You need to specify the the table with new alias to match in your subquery other wise it will match the thread with id from same table
SELECT
m.id,
m.subject,
m.upvotes,
m.downvotes,
(SELECT COUNT(id)
FROM myTable
WHERE myTable.thread = m.id) AS comments_count
FROM myTable m
Or Better to use LEFT JOIN
SELECT
m.id,
m.subject,
m.upvotes,
m.downvotes,
COUNT(mm.id) AS comments_count
FROM myTable m
LEFT JOIN myTable mm ON(mm.thread = m.id)
GROUP BY m.id

Counting MySQL GROUP BY

So, I have next table structure :
Is there a way to make SQL query that counts simillar hashes in r_hash column, than founds this hash in hash column and returns an uid, and count of hashes?
For example - uid - 21520578; type - 1; count - 7?
Try the below query
SELECT T1.uid, T1.type, T2.count
FROM table T1,
(
SELECT r_hash, COUNT(*) AS count
FROM table
GROUP BY r_hash
) T2
WHERE T1.hash = T2.r_hash
You can do that by using join
SELECT t1.uid, t1.type, COUNT(t2.id) as `count`
FROM table AS t1
LEFT JOIN table AS t2 ON t2.r_hash = t1.hash
GROUP BY t1.id
I am not tested this query.
Edit: with left join you also receive rows with count = 0.

Select unmatched records from two tables of MYSQL

I have two tables Table1 and Table2 with some records
id is the common column in both tables and primarykey is set to this column in table1
There are many records in table1 and some of these records (not all) are updated into table2.
Now I want retrieve from table1 the records not updated into the table2.
For example in table1 there are records 1,2,3,4,5,6,7,8,9
And in table2 there are 3,4,7,9
Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2
I wrote this query :
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;
But the expected result not coming. This query lists all the records repeating each one record manytimes
Can any body give me solution to get the expected result.
select *
from table1
where table1.slip_no NOT IN (select id from table2)
Assuming name of common column is id
Or you can modify your query as
SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1]
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
A good reference on SQL joins
SELECT t1.*
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 USING(id)
WHERE
t2.id IS NULL;
You can use the NOT IN operator on a subquery for table2.
Alternatively, use MINUS with two regular queries listing the ids in each table:
SELECT id FROM table1
MINUS
SELECT id FROM table2;
Try this
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME FROM [Table1]
WHERE
NOT EXISTS (SELECT * from Table2 WHERE Table1.SLIPNO !=Table2.id );
You can use the following query
SELECT id FROM database1.table WHERE id NOT IN(SELECT id FROM database2.table)
SELECT child_table.id FROM child_table LEFT JOIN parent_table ON child_table.parent_id = parent_table.id WHERE parent_table.id IS NULL
This left join query returns all the records of the child_table when there is no match in the parent_table. When there is no match, all parent_table fields will be NULL.
inner join will not help. To get unmatched records I tried this:
SELECT
A.ID,A.DATE,A.NAME
FROM TABLE1 A
WHERE CONCAT(A.ID , A.DATE ,A.NAME)
NOT IN
(SELECT CONCAT(B.ID , B.DATE ,B.NAME) as X
from TABLE2 B) ;

Categories