I have a table named Text_Field which consists of a column named ID,
I have another table named Content which consists of a table named value,
I want to fetch those values of ID from the Text_Field table which are present in the value column of the Content and satisfying a said condition.
I know I can construct a query like this
SELECT ID
FROM Text_Field
WHERE ID IN (
SELECT value
FROM CONTENT
WHERE USER='CURRENT_USER')
My only problem is that for some scenarios the value table might contain the ID inside a string
So the inner query might return something like
56789
12334
12348
Rtf(833405)
Now if my ID is 833405 it is present in the value column but the IN query would return false,
I tried
group_concat(value)
So that the inner query returns a single row which is a string,
56789,12334,12348,Rtf(833405)
I want to know that after group_concat can I use something as LIKE to satisfy my need
Or is there some other way I can do this?
Use exists instead, with like:
SELECT t.ID
FROM Text_Field t
WHERE EXISTS (SELECT 1
FROM CONTENT c
WHERE c.USER = 'CURRENT_USER' AND
(c.value = t.id OR
c.value LIKE '%(' || t.id || ')%'
)
);
Note:
Related
Is there any way to do multiple term search in a column using like operator dynamically in SQL Server? Like below
SELECT ID
FROM table
WHERE
Company LIKE '%goog%' OR
Company LIKE '%micros%' OR
Company LIKE '%amazon%'
For example: input values "goog; micro; amazon;" (input value should auto split by delimiter ';' and check the text exist in the table) means that Search term 'goog' or 'micros' or 'amazon' from company column, if exists return.
Table - sample data:
ID Company
------------------------------------------
1 Google; Microsoft;
2 oracle; microsoft; apple; walmart; tesla
3 amazon; apple;
4 google;
5 tesla;
6 amazon;
Basically, The above query should return the results as like below,
Desired results:
ID
-----
1
2
4
6
Is it possible to achieve in SQL Server by splitting, then search in query? I look forward to an experts answer.
If you pass in a table valued parameter, you can join on that.
So for example
CREATE TYPE StringList AS TABLE (str varchar(100));
DECLARE #tmp StringList;
INSERT #tmp (str)
VALUES
('%goog%'),
('%micros%'),
('%amazon%');
SELECT t.ID
FROM table t
WHERE EXISTS (SELECT 1
FROM #tmp tmp
WHERE t.Company LIKE tmp.str);
The one issue with this is that someone could write le; Mic and still get a result.
Strictly speaking, your table design is flawed, because you are storing multiple different items in the same column. You really should have this normalized into rows, so every Company is a separate row. Then your code would look like this:
SELECT t.ID
FROM table t
JOIN #tmp tmp ON t.Company LIKE tmp.str
GROUP BY t.ID
You can simulate it by splitting your string
SELECT t.ID
FROM table t
WHERE EXISTS (SELECT 1
FROM STRING_SPLIT(t.Company) s
JOIN #tmp tmp ON s.value LIKE tmp.str);
I have a table (oracle database if it's important) that looks like this:
NAME VALUE
parent.1 aa1234
parent.2 bb1234
child.3H hh1234
child.2B bb1234
child.6P oo6666
parent.3 hh1234
child.1A aa1234
child.5K ee9999
child.2C bb1234
child.1A aa1234
child.3G hh1234
The table contains parents and children in pretty random order. They referencing each other by the VALUE column. A parent has same string value as it's child. Parents usually have one or more children, so parent and it's children will have exact same value.
This is an example, but it's valid to distinguish parent from child by substring like '%parent%' or like '%child%'. Both NAME and VALUE columns are NVARCHARS2(255).
I'm trying to find orphans - children that don't have a parent by the value (in the example child.6p and child.5K). Is it possible in one query or script?
You can use not exists as follows:
SELECT T.NAME, T.VALUE
FROM YOUR_TABLE T
WHERE T.NAME LIKE '%child%'
AND NOT EXISTS (SELECT 1
FROM YOUR_TABLE T1
WHERE T1.NAME LIKE '%parent%'
AND T.VALUE = T1.VALUE);
select * from tab C
where NAME LIKE ('child%')
AND NOT EXISTS
( SELECT 1 FROM TAB P WHERE
P.NAME LIKE ('parent%')
AND C.VAL = P.VAL
)
SQL Fiddel Demo
I am storing some ids and names in a jsonb array of object like this
[{"id":"1","name":"abc"},{"id":"2","name":"cde"}]
My table looks like this
id userinfo
1 [{"id":"1","name":"abc"},{"id":"2","name":"cde"}]
2 [{"id":"3","name":"fgh"},{"id":"4","name":"ijk"}]
I am trying to select all the records with id 1 but I just want to get ids in userinfo object I don't want names
I tried this
select distinct userinfo->'name' from table where id = 1
but this is giving me null value
This will work with this query
select distinct userinfo->0->'name' from table where id = 1
but I don't know the index so how can I use this query to get my desired result
Thanks
You need to normalize the data by unnesting the array, then you can access each element.
select ui.info ->> 'id' as id,
ui.info ->> 'name' as name
from the_table t
cross join lateral jsonb_array_elements(t.userinfo) as ui(info)
where t.id = 1;
Online example: http://rextester.com/FCNM11312
I have a little problem - since im not very experienced in SQL - about joining the same table on multiple values. Imagine there is table 1 (called Strings):
id value
1 value1
2 value2
and then there is table 2 (called Maps):
id name description
1 1 2
so name is reference into the Strings table, as is description. Without the second field referencing the Strings table it would be no problem, id just do an inner join on Strings.id = Maps.name. But now id like to obtain the actual string also for description. What would be the best approach for a SELECT that returns me both? Right now it looks like this:
SELECT Maps.id, Strings.value AS mapName FROM Maps INNER JOIN Strings ON Strings.id = Maps.name;
But that obviously only covers one of the localized names. Thank you in advance.
You can do this with two joins to the same table:
SELECT m.id, sname.value AS mapName, sdesc.value as description
FROM Maps m INNER JOIN
Strings sname
ON sname.id = m.name INNER JOIN
Strings desc
ON sdesc.id = m.description;
Note the use of table aliases to distinguish between the two tables.
As long as you want to get a single value from another table, you can use subqueries to do these lookups:
SELECT id,
(SELECT value FROM Strings WHERE id = Maps.name) AS name,
(SELECT value FROM Strings WHERE id = Maps.description) AS description
FROM Maps
i need help concerning a sql query. first of all, i have a database with the following structure (example):
ID NAME VALUE
123 ABC_A Text Text Text
123 ABC_A Some more Text
123 ABC_A Even more Text
123 ABC_B some other text
now, i want to get all the different values of rows with the name "ABC_A". i tried to get those via group by and having, without success.
IS this what you want?
SELECT DISTINCT Value
FROM tableName
WHERE ID = 123 AND Name = 'ABC_A'
but if the value of the ID and Name are unique then you can omit distinct (to avoid overkilling the server)
SELECT Value
FROM tableName
WHERE ID = 123 AND Name = 'ABC_A'
Additional to John, if you use the keyword Distinct you onle get DIFFERENT Values, therefore
SELECT DISTINCT Value
FROM tableName
WHERE ID = 123 AND
Name = 'ABC_A'
i want to get all the different values of rows with the name "ABC_A"
This would be for example:
SELECT value, count(value) FROM tbl WHERE name = 'ABC_A' GROUP BY value;
If you do not need the count of times one value appears, remove it, or use DISTINCT:
SELECT DISTINCT value FROM tbl WHERE name = 'ABC_A';
If you want the different values of rows by ID also,
SELECT value, count(value)
FROM tbl
WHERE id = 123 AND name = 'ABC_A'
GROUP BY value;
Or if you want "ALL" the different values (with duplicates too) remove the GROUP BY (and you no longer can use the count(), which would be always 1):
SELECT value FROM tbl WHERE id = 123 AND name = 'ABC_A';
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
In above case why dont you use simple where clause
select * from <tableName> where name ='ABC_A'