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;
Related
I don't have much experience with SQL so forgive me if it is a simple answer...
I am working on a personal project in Python and I am using sqlite3. I currently have a table created like this:
CREATE TABLE IF NOT EXISTS table (id text, username text, score integer, UNIQUE(id,username))
where the idea is two users can score the same ID differently, and also the same user can score multiple ID's.
ID
Username
Score
1
User1
5
1
User2
7
1
User3
10
2
User1
6
2
User2
2
How to I select to find common ID's among users as well as their scores? Is there a select command I can string up to return something like this? (i.e. common between users User1 and User2)
ID
Score User1
Score User2
1
5
7
2
6
2
Use conditional aggregation:
SELECT id,
MAX(CASE WHEN username = 'User1' THEN score END) ScoreUser1,
MAX(CASE WHEN username = 'User2' THEN score END) ScoreUser2
FROM tablename
WHERE username IN ('User1', 'User2')
GROUP BY id
HAVING COUNT(*) = 2;
See the demo.
I am trying to build an sqlite query against a database which I have no control over, i.e. I can't change the odd way data is currently stored.
My data is spread across several tables, and one in particular is causing me issues; contacts.
The tables I am interested in are structured as follows (examples just include what I care about);
Results
id
state
account
5
0
102
11
2
62
Data
id
results_id
type
date
contact_id
1
5
1033
1596664666
360
2
11
1034
1596452446
32
Contacts_list
id
contact_id
key
type
32
12
test
email
360
110
test2
email
360
5
test2
phone
Contacts
id
results_id
name
email
12
231
Test Account
test1#gmail.com
110
726
Test Account 2
test2#gmail.com
5
6
Test Account no
01234567890
So what I want to do is:
Query the data table to grab all the values in that table, lookup type from the relevant table.
join the results table (Data.results_id = Results.id) and lookup the relevant account and state details
Get the contacts name and email from the contacts table.
The last bit is what is throwing me. Data.contact_id = Contacts_list.id and then Contacts_list.contact_id = Contacts.id
If I do a left join on Contacts_list I get additional rows because Contacts_list can have multiple rows for the same ID and I just want as many rows as the Data table has. What I want is to get concat multiple contacts into a single cell, eg;
account
state
type
date
contact
102
0
1033
1596664666
Test Account (test1#gmail.com)
62
2
1034
1596452446
Test Account 2 (test2#gmail.com), Test Account no (01234567890)
I feel like there will be an easy solution, but I am scratching my head at the minute... Any ideas?
You must join all tables, group by data.id and use GROUP_CONCAT() to collect the contacts:
SELECT r.account, r.state, d.type, d.date,
GROUP_CONCAT(c.name || ' (' || c.email || ')') contact
FROM Data d
LEFT JOIN Contacts_list cl ON cl.id = d.contact_id
LEFT JOIN Contacts c ON c.id = cl.contact_id
LEFT JOIN Results r ON r.id = d.results_id
GROUP BY d.id
See the demo.
Hi i have database like this
Users Table
user_id | username | branch_id(foreignkey)| Approved_id(foreignkey)
-------------------------------------------------------------------------------
1 jprescott 2 1
2 lpausch 1 3
Branch Table
Branch_id BranchName
1 abc
2 asjdv
3 ydgfs
Approvetable
ApproveId ApprovedBy
1 Anil
2 ghouse
3 raghu
now i have 3 textboxes,how do i these fields in the textboxes
txtusername.text=username from table 1;
txtapprovedname=approved by from table 2;
txtbranchname=branchname from table 3;
when user 1 logins it should check the branch id and go to the branch table and disply the branch name,and check the approveid and go to the approvedtable and displya the approvedname in textboxes.....
now how to write a select statement for this can any one help me plz...........
select UsersTable.username, ApproveTable.ApprovedBy, Branchtable.BranchName
from UsersTable, Branchtable, Approvetable
where UsersTable.branch_id = BranchTable.branch_id
AND UsersTable.approved_id = ApproveTable.ApproveId
AND UsersTable.User_id = %CurrentUserID%
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.
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.