different search query in sql with different string on column - mysql

let assume one table is having 3 row
id | data
----------
1 | abc
2 | def
3 | ghi
let us assume one string x= abcghilnm
i want to select the row which contain any matching string which contains in string x
so as in above row with id 1 and 3 will be select because it contains abc and ghi which is present in string x
i'm new bie please suggest the sql query for the given situation
thanx..

select id, data
from MyTable
where 'abcghilnm' like concat('%', data, '%')

Related

MySQL, Inserting multiple row from string separated by comma

I understand that I can use the value in insert into to insert multiple rows by columns but my case is a bit different:
I have a string that looks like this: "2,5,14,25,30".
And a connection table that has 2 columns: (INT UserId, INT GroupId).
Let's say I want to connect userId = 1 with all the IDs in the string, how should I do that?
I am familiar with the FIND_IN_SET() function that helps me search for those IDs, but I can't figure how to use it in this case.
In the end, I want my table to look like this:
UserId | GroupId
-----------------
1 | 2
1 | 5
1 | 14
1 | 25
1 | 30

How to join a table from a string of comma delimited string of IDs?

I have a table of values like:
id | genre
-------------
1 | Puzzle
2 | RPG
3 | Action
4 | Fighter
5 | Adventure
And in another table, I have a string of comma delimited Ids that match the above table, like:
'1, 3, 5'
And I want my query to return this field as:
'Puzzle, Action, Adventure'
Also, maybe is there a smarter way to approach this problem than storing this string of ids?

mysql query: search in string position

We have a table which contains card_no information. containing data like:
-----------------------------------------
| id [int(11)] | card_no [varchar(16)] |
-----------------------------------------
| 1 | 0124578965874563 |
| 2 | 1245789658478596 |
| 3 | 8471452369587458 |
-----------------------------------------
Now we need a query to find card number(s) which contains 7 in 6th position. Or which contains 4 in 2nd position.
This is actually needed when we printed card numbers and find some numbers unreadable. so we need to identify the card with rest of the numbers. For example we have data like:
1245_896584_8596
Now we need to identify the card with this data.
Thanks in advance.
You can use function SUBSTRING:
SELECT id, card_no
FROM mytable
WHERE SUBSTRING(card_no, 6, 1) = '7' OR SUBSTRING(card_no, 2, 1) = '4'
Demo here
Use SUBSTR string function
SELECT *
FROM yourtable
WHERE SUBSTR(card_no,2,1) = 4
OR SUBSTR(card_no,6,1) = 7
Use like in where clause and wildcard for exactly one symbol _
Something like
select * from table where card_no like '_____7℅'

How to do group by chunk wise in SQL with negative condition?

I've asked a question here. Now, I need reverse output of it. I've following table, My_Table, with structure as follows,
Name | Address
----------------
Test1 | abc_123
Test1 | abc_456
Test2 | xyz_123
Test1 | xyz_123
Test2 | xyz_456
Test3 | abc_123
Test4 | xyz_123
Test1 | xyz_456
Test3 | xyz_123
Test4 | xyz_456
I need output as follows,
Name
-------
Test2
Test4
I need to do group by chunkwise and select the Name, such that none of the address has prefix as abc.
Output Explanation in detail:
Test1 has addresses as abc_123, abc_456, xyz_123, abc_123 and at least one of the address has prefix as abc. Hence not in output.
Test2 is has addresses as xyz_123, xyz_456 and none of them has prefix abc. Hence in output.
x = select distinct(Name) from My_Table
y = select distinct(Name) from My_Table where Address like 'abc%'
Result = x - y
I was able to achieve result using 2 queries (as shown above) and then using subtract operation in the result set, but can this be achieved in single query?
Note: I'm using JAVA for query MYSQL DB, hence I can subtract result sets. My table size is huge, hence I want to optimize number of queries!
One method uses aggregation and is similar to the other answer:
select name
from my_table
group by name
having sum(address like 'abc%') = 0;
The having clause counts the number of matching rows. The = 0 says there are none for a given name.

MySQL: Merge rows and returning default values

Imagine the following table:
id | variant | name
-----------------------
1 | 1 | a
1 | 2 | b
1 | 3 | c
2 | 1 | d
2 | 2 | e
2 | 3 | NULL
3 | 1 | g
Which SQL statement do I need to run to get this:
For a given id and a given variant get the name of this combination.
But if the name is NULL get the name of the row with the given id but with variant 1 (the name of variant 1 is never NULL).
If there is no row with the given variant, again, use the row where variant is 1 with the same id.
Variant 1 is never requested directly.
This is like a fall back mechanism. Or you could consider it as overriding values of rows with variant = 1.
Examples:
id = 1, variant = 2: b
id = 1, variant = 3: c
id = 2, variant = 3: d
id = 3, variant = 5: g
Is this possible with SQL? And is it performing well if the mechanism is applied on many more fields?
Thanks!
Update:
Please note that I would like to have this mechanism not only for the name field. There should be further columns which should have the same behaviour - but each column should be treated on its own.
This should do what you need using a LEFT JOIN to get the optional value.
SELECT COALESCE(b.Name,a.Name)
FROM Table1 a
LEFT JOIN Table1 b
ON a.id=b.id AND b.variant=#y
WHERE a.id=#x AND a.variant=1
An SQLFiddle to test with.
Performance wise, it would depend on how you need to apply the query to get multiple fields. If you can solve your column choice using COALESCE from the existing join, I can't see a big problem, but if you end up with multiple self joins to solve it, you may have a problem.
As always, performance will depend on your data structure, how many rows you have, and what indexes you have created. I would recommend a query like this:
SELECT name FROM table WHERE id=#1 AND ((variant=#2 AND name IS NOT NULL) OR variant=1) ORDER BY variant DESC LIMIT 1