MySQL: Find "same" Data - mysql

I am currently in the need to find entrys matching the same pattern in a connection table.
The Table looks like
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
3 1 1 5
4 1 2 4
5 5 1 3
6 5 2 7
so my basic information is the data of job_id 15
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
I want to find job_id 5 because the data in ext_id and data1 is the same as in job 15. the data of job_id 1 differs, so I don't want to find that.
Any idea on how to do it?

I believe you want this:
select *
from your_table
group by data1,
ext_id
having count(*) > 1
This post explains it:
How to find duplicates in 2 columns not 1
EDIT
I believe this should return all rows that have mathcing data1 and ext_id values
select * from table t1
INNER JOIN table t2 ON t1.data1=t2.data1 and t1.ext_id=t2.ext_id

Related

SQL - selecting count of different value

Assume I have table count and the column is (nilai, id_courses, id_lecturer)
nilai id_courses id_lecturer
----- ---------- -----------
2 1 1
2 1 1
2 1 1
3 1 1
3 1 1
1 2 1
1 2 1
5 2 1
5 2 1
then I want to create view like this :
nilai id_courses id_lecturer count
----- ---------- ----------- -----
2 1 1 3
3 1 1 2
1 2 1 2
5 2 1 2
how to do that in SQL syntax?
I just know how to count 1 value with this code
SELECT COUNT( nilai ) , id_courses, id_lecturer FROM count where nilai=1
I've read this post but its to complex, so I don't know how it's work
You need to count all distinct entries by grouping them. The query
SELECT nilai, id_courses, id_lecturer, COUNT(*) AS count
FROM count GROUP BY nilai, id_courses, id_lecturer
should exactly return the table you posted.

Check value in rows with same FK

I have the table below
ID | PARCEL | STATUS | ORDER_ID
1 1 COMPLETE 1234
2 2 COMPLETE 1234
3 1 COMPLETE 9999
4 2 PENDING 9999
5 3 PENDING 9999
6 1 COMPLETE 1111
7 2 COMPLETE 1111
8 3 COMPLETE 1111
9 1 COMPLETE 3333
10 2 PENDING 3333
I need select the order_id with ALL PARCEL's having a COMPLETE status.
i am trying with select, but don't work
SELECT * FROM table WHERE order_id NOT IN
(SELECT order_id FROM table WHERE status = 'COMPLETE');
the answer for the query is
ID | PARCEL | STATUS | ORDER_ID
1 1 COMPLETE 1234
2 2 COMPLETE 1234
6 1 COMPLETE 1111
7 2 COMPLETE 1111
8 3 COMPLETE 1111
You can use NOT EXISTS clause, so it will skip any order_id that has status other than COMPLETE
SELECT * FROM tableA A1
WHERE NOT EXISTS
( SELECT 1 from tableA A2
where A1.order_id= A2.order_id
and A2.status <> 'COMPLETE'
)
select * from table_parcels where STATUS like 'COMPLETE';

Join in MySQL with an UPDATE condition for multiple rows

I'm trying to perform a UPDATE JOIN query in MySQL
I need to do the following: Add the table_1.won to table_2.total_winnings for a given session
+++ Table_1 +++
--id-- --name-- --selection-- -potential_winnings-- -- won -- --session--
1 John a 67 0 1
2 Jame b 10 **10** 1
3 David c 43 0 1
4 Sam b 20 **20** 1
5 Alex b 30 **30** 1
6 Rob b 1000 0 2
+++ Table_2 +++ (BEFORE)
--id-- --Total_winnings-- -- session --
1 4534 1
2 885 1
3 0 1
4 5 1
5 10 1
6 5465 2
My desired output is below
input : winning selection = b
session =1
+++ Table_2 +++ (AFTER)
--id-- --Total_winnings-- -- session --
1 4534 1
2 **895** 1
3 0 1
4 **25** 1
5 **40** 1
6 5465 2
I can do this by selecting each user from table_1 who has won and looping over there entry in table_2, but I have a large number of items to process now, so I think I need a join of somesort to accomplish this..
I'm currently doing
UPDATE table_2 SET Total_winnings = Total_winnings + 10 WHERE id = 2 AND session = 1
If anyone would know how to do this, or has a simple example of a SQL join with and UPDATE query that would be most useful. I have seen other examples of this, but I can never figure out what it going on in the SQL!!
You're looking for something like this?
UPDATE table_2
join table_1 on table_1.id = table_2.id
SET Total_winnings = Total_winnings + won
WHERE session = 1 and selection = 'b'

How to get the count of rows from a table in cakephp

I have two tables:
id category status
1 test 1
2 test1 1
3 test2 1
This is the group_master table.
groupid groupname groupmaster
1 yy 1
2 xx 1
3 yyyy 1
4 xrx 1
5 yy 2
6 xx 2
7 yyyy 2
8 xfgdrx 3
This is the membergroup table.
The group_master.id is same as in membergroup.groupmaster.That menas i want to get each row from first table and also want to get the count from second table
That means:
id category status Count
1 test 1 4
2 test1 1 3
3 test2 1 1
This is the result i want to get.
How can i do this in Cakephp with pagination ?
try this:
You need to JOIN both tables and do a GROUP BY
SELECT g.id,g.category,g.status,count(*) as Count
FROM group_master g
JOIN membergroup m
ON g.id=m.groupmaster
GROUP BY g.id,g.category,g.status
SQL Fiddle Demo

mysql select most recent, limit by source

I have a table with the following fields:
id
source_id
title
date
I want to select the 25 most recent items, so SELECT * FROM table ORDER BY date DESC LIMIT 50
The extra requirement is to select only the 3 most recent from every source_id.
So if the records look something like that,
id | source_id | title | date
----+-----------+-------+---------
1 2 aaa 2012-1-1
2 2 aaa 2012-1-2
3 2 aaa 2012-1-3
4 2 aaa 2012-1-4
5 3 aaa 2012-1-5
6 4 aaa 2012-1-6
I want my query to return items 4,3,2,5,6
So just the 3 most recent of every source with an over all limit of 25.
I'm not sure it's clear enough so please ask if you need more details.
Here you go:
SELECT *
FROM your_table t1
WHERE
(
SELECT COUNT(*)
FROM your_table t2
WHERE
t1.source_id = t2.source_id
AND t1.date < t2.date
) < 3
ORDER BY source_id, date DESC
Result:
4 2 aaa 2012-01-04
3 2 aaa 2012-01-03
2 2 aaa 2012-01-02
5 3 aaa 2012-01-05
6 4 aaa 2012-01-06
In plain English: take only rows that have less than 3 newer rows with the same source_id.
NOTE: This could select more than 3 rows per source_id if the third newest date (for the same source_id) happens to be shared by more than one row. Let me know what "3 newest" means in this context if that's a problem...
select * from table where source_id in
(select distinct source_id from table order by date limit 3)
LIMIT 25