help in sql command .. i want list names which have defined id s - sql

i have table consist of columns : id,name,job
and i have row stored with this data :
id: 1
name : jason
job: 11,12
id: 2
name : mark
job: 11,14
i want write sql command to fetch names which have value "14" stored in job column only from this table
so how i do that ?
thanks

You can do:
WHERE FIND_IN_SET(14, job)
But that is really not the correct way. The correct way is to normalize your database and separate the job field into its own table. Check this answer for extra information:
PHP select row from db where ID is found in group of IDs

You shouldn't be storing multiple job ids in the same field. You want to normalise your data model. Remove the 'job' column from your names table, and have a second JOB table defined like this:
id | name_id | job_id
1 1 11
2 1 12
3 2 11
4 2 14
where name_id is the primary id ('id') of the entry in the names table.
Then you can do:
SELECT name_id, job_id FROM JOB WHERE name_id = 1;
for example. As well as making your data storage far more extensible - you can now assign unlimited numbers of job_ids to each name for example - it'll also be much faster to execute queries as all your entries are now ints and no string processing is required.

SELECT
*
FROM
MyTable
WHERE
job LIKE '14,%' OR job LIKE '%,14' OR job LIKE '%,14,%'
EDIT: Thanks to onedaywhen
SELECT
*
FROM
MyTable
WHERE
(',' + job + ',') LIKE ('%,14,%')

Related

How to get a id value of tables in postgres

How to get a unique, identical value of a table?
For example, if there are tables like 't_aa', 't_bb', 't_cc', I want a result like below.
id | table_name
-------------------
1 | 't_aa'
2 | 't_bb'
3 | 't_cc'
What I exactly want is to get a specific, and unique number from the name of tables.
I have tried
SELECT * FROM information_schema.tables;
-- or
SELECT * FROM pg_catalog.pg_tables;
but this doesn't provide any identical numbers to me.
I hope there is some way to get results like above by using some lines of query,
but if I really have to make a new table for this, that could be okay as an alternative.
please help me, thank you
-- edit
I need numbers because I will use it as an advisory lock key for some reasons.
ThIs is it:
SELECT table_name,ROW_NUMBER () OVER (
ORDER BY table_name
) as id FROM information_schema.tables;

How can I use an input from another table in my query?

I'm creating a new table using PostgreSQL, but I need to get a parameter from another table as an input.
This is the table I have (I called table_1):
id column_1
1 100
2 100
3 100
4 100
5 100
I want to create a new table, but only using ids that are higher than the highest id from the table above (table_1). Something like this:
insert into table_new
select id, column_1 from table_old
where id > (max(id) from table_1)
How can I do this? I tried searching, but I got to several posts like https://community.powerbi.com/t5/Desktop/M-Query-Create-a-table-using-input-from-another-table/td-p/209923, Take one table as input and output using another table BigQuery and sql query needs input from another table, which are not exactly what I need.
Just use where id > (select max(id) from table_1).

Selecting rows based on values in one of its semi-colon delimited columns: PL/SQL

I'm currently trying to select all rows where a certain ID exists within that rows semi-colon delimited ID column.
The Table:
=====================
TOOL_ID | TOOL_USERS
---------------------
1 1;2;3
2 1;3
3 1
=====================
I want to select all the tools that a certain user has access to, for example, the desired result where the user ID is 3:
TOOL_ID | TOOL_USERS
---------------------
1 1;2;3
2 1;3
I know that this design is not normalized, but I do not have the ability to change/modify the database. I could always just query all of the rows and then loop through the results deleting any that don't contain the user id, but I'd rather do this is one nice, clean query.
Is this possible?
Thanks.
You can use the LIKE keyword with wildcards. I included leading and ending semicolons so 13 and stuff doesn't match 3.
SELECT TOOL_ID, TOOL_USERS FROM YourTable WHERE ';' || TOOL_USERS || ';' LIKE '%;3;%'
Someone let me know if I didn't translate this well to PLSQL.

Return query name Access SQL

In an already existing table I would like to create a new column containing the name of the query I am using.
For example:
ID Name
-----------
1 Max
2 Jack
The desired output is:
ID Name Query
-------------------
1 Max QueryName
2 Jack QueryName
I think you can use a static value field in your query, like this:
SELECT ID, NAME, "QueryName" AS Query
FROM yourTable;
When you just want to add a column with a static value in result of querying a table, above is the solution, If you want to create a Query with a special name then use it, just use its name in its query in creation time.
I mean when you are changing name of a query; also edit its query with using that name.
But, I don't think this is a good idea to store a query then change its name and use its name as a result of its query or other queries, It seems you want to store a string value somewhere then use it by adding it in another query, So:
Create another table(QueryTable) like:
QueryId | QueryName
--------+--------------
1 | Query Name 1
2 | Query Name 2
Use it in your other queries:
SELECT t.*, q.QueryName
FROM yourTable t CROSS JOIN QueryTable q
WHERE q.QueryId = 1;
And I suggest this because it's better to use a tool in its using zone, when you want to store data and use it result some information; use a table for that. HTH ;).

Fetch a single field from DB table into itab

I want to fetch the a field say excep_point from a transparent table z_accounts for the combination of company_code and account_number. How can I do this in ABAP SQL?
Assume that table structure is
|company_code | account_number | excep_point |
Assuming you have the full primary key...
data: gv_excep_point type zaccounts-excep_point.
select single excep_point
into gv_excep_point
from zaccounts
where company_code = some_company_code
and account_number = some_account_number.
if you don't have the full PK and there could be multiple values for excep_point
data: gt_excep_points type table of zaccounts-excep_point.
select excep_point
into table gt_excep_points
from zaccounts
where company_code = some_company_code
and account_number = some_account_number.
There is at least another variation, but those are 2 I use most often.
For information only. When you selects data into table you can write complex expressions to combine different fields. For example, you have internal table (itab) with two fields "A" and "B". And you are going to select data from DB table (dbtab) wich have 6 columns - "z","x","y","u","v","w". And for example each field is type char2 You aim to cimbine "z","x","y","u" in "A" field of internal table and "v","w" in "B" field. You can write simple code:
select z as A+0(2)
x as A+2(2)
y as A+4(2)
u as A+6(2)
v as B+0(2)
w as B+2(2) FROM dbtab
INTO CORRESPONDING FIELDS OF TABLE itab
WHERE <where condition>.
This simple code makes you job done very simple
In addition to Bryans answer, here is the official online documentation about Open SQL.