Find an entry in sql dependent on other entries in the table - mysql

How can I return all entries in a table that contain a string in a given column if the same string is also present in the same column with a given suffix ?
So for example, given the suffix 'bar', and the following table:
id A
1 foo1
2 foo2
3 foo1bar
I want to return the the first entry, because there's another entry (the third one) which has the same value when the suffix is appended.

SELECT t1.*
FROM table t1
JOIN table t2 ON CONCAT(t1.column, 'bar') = t2.column

I would suggest using exists:
select t.*
from t
where exists (select 1
from t t2
where t2.a = concat(t.a, 'bar')
);
I recommend this specifically over a join to avoid returning duplicate values, in the event that multiple rows match the conditions.

Related

fetch id from table1 based on some conditions and check if that id exists in other table

I have 2 tables; alternate identifier table and a master table
the above image represents the column from alternate identifiers table
and master table have these columns
Now, First I fetch company_group_id from alternate identifier table where bank_entity_id='somevalue' and id_value = 'somevalue';
if this query return nothing than end. if this query return company_group_id than I will check that company_group_id in master table where company_group_id = selected company group id.
after getting this if its present in the table than i will check hashcode if it presents in the master table.
separate queries are like this:
select company_group_id from aes_batch.aes_company_group_alternate_identifiers
WHERE ID_VALUE = '525' and BANK_ENTITY_ID='UOBS';
select company_group_id, hashcode from aes_batch.aes_company_group_master where company_group_id = 'value from the last query' and hashcode="value";
I want to combine these queries to get same result.
this is what I tried but failed.
SELECT t1.company_group_id
FROM aes_batch.aes_company_group_alternate_identifiers t1
LEFT JOIN aes_batch.aes_company_group_master t2
ON t1.company_group_id = t2.company_group_id
WHERE t2.company_group_id IS NULL;
SELECT *
FROM aes_batch.aes_company_group_master B
WHERE NOT EXISTS (SELECT 1
FROM aes_batch.aes_company_group_alternate_identifiers A
WHERE B.company_group_id = A.company_group_id);
Select a.company_group_id, b.company_group_id,
b.hashcode, a.id_value
from aes_batch.aes_company_group_alternate_identifiers a
LEFT JOIN aes_batch.aes_company_group_master b
ON b.company_group_id = a.company_group_id
WHERE a.id_value in ('524','525')
and a.bank_entity_id='UOBS';
can someone help me in this?

Sql select where array in column

In my query I use join table category_attributes. Let's assume we have such rows:
category_id|attribute_id
1|1
1|2
1|3
I want to have the query which suites the two following needs. I have a variable (php) of allowed attribute_id's. If the array is subset of attribute_id then category_id should be selected, if not - no results.
First case:
select * from category_attributes where (1,2,3,4) in category_attributes.attribute_id
should give no results.
Second case
select * from category_attributes where (1,2,3) in category_attributes.attribute_id
should give all three rows (see dummy rows at the beginning).
So I would like to have reverse side of what standard SQL in does.
Solution
Step 1: Group the data by the field you want to check.
Step 2: Left join the list of required values with the records obtained in the previous step.
Step 3: Now we have a list with required values and corresponding values from the table. The second column will be equal to required value if it exist in the table and NULL otherwise.
Count null values in the right column. If it is equal to 0, then it means table contains all the required values. In that case return all records from the table. Otherwise there must be at least one required value is missing in the table. So, return no records.
Sample
Table "Data":
Required values:
10, 20, 50
Query:
SELECT *
FROM Data
WHERE (SELECT Count(*)
FROM (SELECT D.value
FROM (SELECT 10 AS value
UNION
SELECT 20 AS value
UNION
SELECT 50 AS value) T
LEFT JOIN (SELECT value
FROM Data
GROUP BY value) D
ON ( T.value = D.value )) J
WHERE value IS NULL) = 0;
You can use group by and having:
select ca.category_id
from category_attributes ca
where ca.attribute_id in (1, 2, 3, 4)
group by ca.category_id
having count(*) = 4; -- "4" is the size of the list
This assumes that the table has no duplicates (which is typical for attribute mapping tables). If that is a possibility, use:
having count(distinct ca.attribute_id) = 4
You can aggregate attribute_id into array and compare two array from php.
SELECT category_id FROM
(select category_id, group_concat(attribute_id) as attributes from category_attributes
order by attribute_id) t WHERE t.attributes = (1, 2, 3);
But you need to find another way to compare arrays or make sure that array is always sorted.

use values of two fields of a first SELECT into a second one

Let's say we have a database with a table defined by the following fields:
id -- localCode -- localNumber -- status
Hoping to help you: there will be different records with the same combination (localCode + localNumber) but different status.
I would like to use the results from this query
SELECT localCode, localNumber FROM Table WHERE status = 'val_1'
to find the records with the combination (localCode + localNumber) found before but status = 'val_2'.
Thank you all
Make a self-join on the table.
SELECT
t2.id
FROM Table t1
INNER JOIN Table t2
ON t1.localCode = t2.localCode
AND t1.localNumber = t2.localNumber
WHERE t1.status = 'val_1'
AND t2.status = 'val_2'
This query will find all IDs of records that have status val_2 and where a corresponding entry with status val_1 exists.

Cartesian product of- records of a single column on single table

Hi I would like to know whether, is it possible to do a 'Cartesian product' operation
on column records. Here is the scenario:
PIID1 PIID2
PIID11 (Intially empty)
PIID12 (Initially empty)
PIID13 (Initially empty)
required output(removed self relations) :
PIID1 PIID2
-------- --------------------
PIID11 PPIID12
PIID11 PPIID13
PPIID12 PIID11
PPIID12 PPIID13
PPIID13 PPIID12
PPIID13 PPIID11
please post your solutions .
First insert the new rows with a self-join:
INSERT INTO tableX
(piid1, piid2)
SELECT
a.piid1, b.piid1
FROM
tableX a
CROSS JOIN
tableX b
WHERE
a.piid1 <> b.piid1 ;
Then delete the original rows:
DELETE FROM tableX
WHERE piid2 IS NULL ;
Try SELF JOIN:
Select
a.pIID1 as PIID1,
b.pIID1 as PIID2
from
table a,
(Select pIID1 from table) b
WHERE a.pIID1<>b.PIID1

sql query for deleting rows with NOT IN using 2 columns

I have a table with a composite key composed of 2 columns, say Name and ID. I have some service that gets me the keys (name, id combination) of the rows to keep, the rest i need to delete. If it was with only 1 row , I could use
delete from table_name where name not in (list_of_valid_names)
but how do I make the query so that I can say something like
name not in (valid_names) and id not in(valid_ids)
// this wont work since they separately dont identity a unique record or will it?
Use mysql's special "multiple value" in syntax:
delete from table_name
where (name, id) not in (select name, id from some_table where some_condition);
If your list is a literal list, you can still use this approach:
delete from table_name
where (name, id) not in (select 'john', 1 union select 'sally', 2);
Actually, no I retract my comment about needing special juice or being stuck with (AND OR'ing all your options).
Since you have a list of values of what you want to retain, dump that into a temporary table. Then do a delete against the base table for what does not exist in the temporary table (left outer join). I suck at mysql syntax or I'd cobble together your query. Psuedocode is approximate
DELETE
B
FROM
BASE B
LEFT OUTER JOIN
#RETAIN R
ON R.key1 = B.key1
AND R.key2 = B.key
WHERE
R.key1 IS NULL
The NOT EXISTS version:
DELETE
b
FROM
BaseTable b
WHERE
NOT EXISTS
( SELECT
*
FROM
RetainTable r
WHERE
(r.key1, r.key2) = (b.key1, b.key2)
)