compare rows of two query set result in same table - php

I have table where I store data on basis of date.
Now I need to check that: is there any difference between data of rows with two different dates.
In a simple way you can say that I have two queries which select data from same table now I have to compare each row and column value. for example my two query are -
SELECT * FROM `national` WHERE `upload_date` = '2015-08-04' // return 106 rows
and
SELECT * FROM `national` WHERE `upload_date` = '2015-08-01' // return 106 rows
I have tried to compare this with below query but the result not seem to be correct to me, I am not satisfy with this.
Select U.* from
(SELECT t1.* FROM `national` t1 where upload_date = '2015-08-01'
union all
SELECT t2.* FROM `national` t2 WHERE `upload_date` = '2015-08-04' ) U
GROUP BY emp_id, uqi_id
HAVING COUNT(*) = 1
Can Any one please provide me correct query ?? thank you

Try this
(
SELECT t1.*
FROM
`national` t1, `national` t2
where
t1.upload_date = '2015-08-01' and t2.upload_date='2015-08-04' and
-- put your columns here that you want to compare for same DATA
-- like t1.name=t2.name and etc...
)

You can try with something like that
SELECT * FROM (SELECT * FROM `national` WHERE upload_date = '2015-08-01') a
INNER JOIN (SELECT * FROM `national` WHERE upload_date = '2015-08-04') b
ON a.emp_id = b.emp_id AND a.uqi_id = b.uqi_id
ORDER BY uqi_id

Related

PHP SQL Query Fields from Another Table Join

I am trying to get a query using fields from 2 tables.
I need to query Table1 but only Table2 has the variable venue_location that I need to query.
Basically I need to count all records on Table1 where Table1.venue_location = $MyVariable.
Here is what I've put together but I believe I need to use Joins for this?
Table1
- venue_id
Table2
- venue_id,
- venue_location
SELECT * FROM Table1 WHERE table1.venue_id = table2.venue_id and table2.location = '$MyVariable'
How can I do a query for this?
Use the power of join table
SELECT * FROM Table1
JOIN Table2 USING(venue_id)
WHERE table2.location = '$MyVariable'
You can get back the count of rows with mysqli_num_rows() in PHP, or change le select by SELECT COUNT(*) AS nbRow FROM ... and check of value in nbRow column
You can join two tables on venue_id and then group it by venue_id where location is your $MyVariable.
Your final query will look like:
SELECT count(table2.venue_id)
FROM Table1
JOIN Table2 ON table1.venue_id = table2.venue_id
WHERE table2.location = '$MyVariable'
GROUP BY table2.venue_id
try this
SELECT Table1.venue_id, Table2.venue_location FROM Table1 INNER JOIN Table2
ON Table1.venue_id='$MyVariable';

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.

How can I build this query to compare a id with two tables?

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

MySQL Select Count with EXISTS

I have 3 tables.
table1
id, thing_id
table_index
id
table_index_info
table_index_id, table1_id
table_index_info contains a history of table_index. This history can refer to table1, possibly many times or 0 times.
I need to run a query that returns all rows in table1 with a specific thing_id.
It also needs to count how many rows in table_index that have at least 1 table_index_info linking to table1.
Here's my query:
SELECT table1.*,
(SELECT COUNT(i.id)
FROM table_index i
WHERE EXISTS (SELECT 0
FROM table_index_info h
WHERE h.table1_id = table1.id
AND h.table_index_id = i.id)
) AS indexCount
FROM table1
WHERE table1.thing_id= $thingId
Is this the best/correct way to do this?
I would use a JOIN instead of EXISTS in this case.
SELECT table1.*,
( SELECT COUNT(i.id)
FROM table_index i
INNER JOIN table_index_info h ON h.table_index_id = i.id
WHERE h.table1_id = table1.id
) AS indexCount
FROM table1
WHERE table1.thing_id = $thingId

How to Remove matching rows From mysql Join Result and show non-matching rows?

Is there any way to remove the matching rows from MySQL join Query.
Actually I have two tables where I have store the pub_id, and post_id in both tables these are common.
I want a result when I query all the matching rows from table1 and table2 should not be listed and the non-matching rows should be listed only.
Query return rows which exists only in one of two tables:
SELECT *
FROM Table1 t1
WHERE NOT EXISTS (Select 1 from Table2 t2
Where t1.pub_id = t2.pub_id
AND t1.post_Id = t2.post_id)
UNION ALL
SELECT *
FROM Table2 t1
WHERE NOT EXISTS (Select 1 from Table1 t2
Where t1.pub_id = t2.pub_id
AND t1.post_Id = t2.post_id)
you need something like that:
SELECT * FROM tablea AS a
RIGHT JOIN tableb AS o ON a.id = o.id WHERE a.pub_id IS NULL and a.post_id is null
UNION
SELECT * FROM tablea AS a
LEFT JOIN tableb AS o ON a.id = o.id WHERE o.pub_id IS NULL and o.post_id is null

Categories