Here is my table and data of these tables
Table name: Code
CID Code
1 abc
2 def
3 xyz
Table Name : Details
ID Name CID
1 a 1
2 b 2
Resultant Table:
ID Code Name
1 abc a
2 abc Null
3 def b
4 def Null
5 xyz Null
6 xyz Null
I nned to get all record from the code table and against each code I have to get all the rows from the details table, if some code have value their need value and if not then Null
Thanks
Sounds like you're looking for the cartesian product:
SELECT
c.CID * d.ID AS ID,
c.Code,
CASE
WHEN c.CID = d.CID THEN d.Name
ELSE NULL
END AS Name
FROM Code c
CROSS JOIN Details d
Although cartesian products are quite slow for larger tables... so be sure that this is what you really want.
Related
According to documentation Sqlite SUM() function returns NULL if no row in table meets criteria.
I don't want any lines to return if the id does not exist, like in the query:
SELECT SUM(tMoney.money),tCustomer.name FROM tMoney JOIN tCustomer ON tMoney.id = tCustomer.id WHERE tCustomer.id = 3
tMoney
id money
--- ------
1 210
2 400
1 150
tCustomer
name id
--- ------
bob 1
dan 2
Just filter out lines with null using "having" clause
SELECT SUM(tMoney.money), tCustomer.name
FROM tMoney
JOIN tCustomer
ON tMoney.id = tCustomer.id
WHERE tCustomer.id = 3
group by tCustomer.name
having SUM(tMoney.money) is not null;
This is a beginner question (I am not to good with databases at the moment)
But here goes I have 2 tables
Table 1
User
id - name - status_id
0 John 0
1 Jim 1
2 Stan 0
Table 2
Status
id - name
0 employed
1 Not employed
I want to make a query that gives me Status name from User id.
Join the two tables on User.status_id and Status.id. Then select the user you are interested in.
select s.name
from User as u,
Status as s
where u.status_id = s.id
and u.id = 1;
I am stuck with a query in an Sqlite Database.
The complete table structure is a bit complex so I will make an example that is broken down to my Problem.
I have 2 tables like
T1
_id name v1 v2 v3
1 test 4 3 1
2 to 1 2 2
3 show 2 2 2
4 what 4 2 4
5 I 1 1 1
6 mean 3 3 1
T2
_id name
1 this
2 is
3 a
4 test
v1, v2, v3 are the foreign keys of T2 _id and in combination will result in one string.
In this example:
T1 _id 1: testathis
T1 _id 2: thisisis
T1 _id 3: isisis
T1 _id 4: testistest
T1 _id 5: thisthisthis
T1 _id 6: aatest
And in this resulting string I want to search and get a result table.
For example if the search string is "isi" (in the sql query "%isi%") I want to get a result table like this:
name v1v2v3
to thisisis
show isisis
It is propably quit easy but I am totally stuck here.
I tried already several ways including group_concat() and various combinations of select ... where clauses.
Although I tried a query similar to this one.
But I am allways failing on the fact that the string has to be concated from rows of another table.
Karl, give this is try:
select t1.name, t21.name || t22.name || t23.name as v1v2v3 from t1
join t2 t21 on t1.v1 = t21._id
join t2 t22 on t1.v2 = t22._id
join t2 t23 on t1.v3 = t23._id
where t21.name || t22.name || t23.name like '%isi%'
I don't have SQLite to test it right now, but it should work :)
SELECT
T1.name as name,
tmpV1.name || tmpV2.name || tmpV3.name as v1v2v3
FROM
T1
JOIN
T2 as tmpV1 on T1.v1 = tmpV1._id
JOIN
T2 as tmpV2 on T1.v2 = tmpV2._id
JOIN
T2 as tmpV3 on T1.v3 = tmpV3._id
WHERE
v1v2v3 like '%isi%'
;
Edit: aww Mosty beat me.
I have following two tables:
ID_PERSON NAME
-----------------
1 John
2 Joe
3 Peter
ID_PERSON ID_SPECIALIZATION
------------------------------
1 5
1 6
1 7
2 5
2 1
3 6
3 10
I need to filter data based on group of ids ID_SPECIALIZATION that will be provided. For example
I want to display only those persons who has specialization in 5 and 6 so it will return only first person. In ASP.NET Web form there will be two listboxes, left and right button, in first LB there will be all possible specializations and user will choose some of them to second LB as filtering options. I have no idea how to put this filtering condition in sql query. Thanks for help.
You could use the following:
SQL> SELECT p.id_person, p.NAME
2 FROM person p
3 JOIN person_spe s ON p.id_person = s.id_person
4 WHERE id_specialization IN (5, 6)
5 GROUP BY p.id_person, p.NAME
6 HAVING COUNT(*) = 2;
ID_PERSON NAME
---------- -----
1 John
One way to do it:
SELECT
ID_PERSON
, NAME
FROM
Person AS p
WHERE EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 5
)
AND EXISTS
( SELECT *
FROM
PersonSpecialization AS ps
WHERE ps.ID_PERSON = p.ID_PERSON
AND ps.ID_SPECIALIZATION = 6
)
SELECT d1.id_person, d1.name FROM tbl_table1 d1
INNER JOIN tbl_table2 d1
ON d1.ID_PERSON=d2.ID_PERSON
WHERE ID_SPECILIZATION = ?
Theres the query but I'm not sure how asp.net works and passing in the value. It might be work looking up bind variables which allows you to use place holders in the sql which oracle then caches the query and just uses the values that you pass in at run tuime using EXECUTE IMMEDIATE.
I'm looking to delete all entries that are referenced by a record, and all the children in different tables as well. If possible I'd like to use a multi-table delete statement as opposed to triggers.
For example
Table: forms
id var
1 foo
2 bar
Table 2: form_options
id form_id var
1 1 blah
2 2 hello
3 2 world
Table 3: form_options_info
id form_options_id var
1 3 world info
So given the above type of table struct, if I delete row 2 from forms that would delete row 2,3 from form_options as well as row 1 from form_options_info.
Maybee not the best solution, but it works:
DELETE FROM form_options_info, form_options, forms
USING forms INNER JOIN form_options INNER JOIN form_options_info
WHERE (form_options_info.form_options_id = form_options.id
AND form_options.form_id = forms.id
OR form_options.form_id = forms.id)
AND forms.id = 2;
...or just change the tables to InnoDB ;-)...