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%
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.
So I am using asp.net mvc5 . I used database first approach to create simple web application. My question is how can I create child record when parent record is created. Let's say I have 3 tables that is (1)Students (2)Subject and (3)StudentSubject.
Sample data can be
students Subjects StudentSubject
-------------- --------- ----------------
ID Name SID SName ID SID
-------------- --------- ----------------
1 St1 1 Sub1 1 1
2 St2 2 Sub2
3 Sub3
4 Sub4
So in above example student "St1" is enrolled in subjects "Sub1" .
I am able to create 3 views that can create new student, new subject and even new record in table StudentSubject.
But my question is how can I create records in table StudentSubject at the same time when I create record in table Student.
So my view can have fields like
ID,
name,
SubjectName
and if I enter
ID 3
name St3
SubjectName Sub2
and I click save, it should first create record in table Students and then in table StudentSubject based on subject selected.
So now the tables should look as below
students Subjects StudentSubject
-------------- --------- ----------------
ID Name SID SName ID SID
-------------- --------- ----------------
1 St1 1 Sub1 1 1
2 St2 2 Sub2 3 2
3 St3 3 Sub3
4 Sub4
Thank You.
Without sharing any of your code it is hard to tell what your problem is. Here is my best guess at a solution without seeing any of your code:
using (var context = new MyContext())
{
var myNewStudent = new Student()
{
//initialize your student
};
context.Students.Add(myNewStudent);
context.SaveChanges();
//get the identity column from the inserted student
int id = myNewStudent.ID;
var myNewStudentSubject = new StudentSubject()
{
SID = id
}
context.StudentSubjects.Add(myNewStudentSubject);
context.SaveChanges();
}
Perhaps you should explain what you mean by "at the same time" on these inserts, and why you think it is necessary.
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 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 doing a test web shop that has sections within sections. There can be an unlimited number of levels so I have just one table Section.
The table has the following columns:
SectionID, SectionTitle, SectionLevel, ParentID, PageOrder
SectionLevel: 1 being topmost (no parent)
PageOrder: Within it's parent group, which order it should go in.
And for test data :
SectionID SectionTitle SectionLevel ParentID PageOrder
--------- ------------ ------------ -------- ---------
2 Ladies 1 0 2
3 Mens 1 0 3
4 Jewellery 2 2 1
5 Clothing 2 2 2
6 Clothing 2 3 1
7 Accessories 2 3 2
I want to return this data in one table so that the first top level section is first, and then all of it's children's sections are next, and then the second top level section etc.
I've had a play around with it but can't get to come out right. I think that it should be possible to do it if I redesigned the table but can't think how.
The data should come back in the following order:
SectionID SectionTitle SectionLevel ParentID PageOrder
--------- ------------ ------------ -------- ---------
2 Ladies 1 0 2
4 Jewellery 2 2 1
5 Clothing 2 2 2
3 Mens 1 0 3
6 Clothing 2 3 1
7 Accessories 2 3 2
WITH tree (SectionID, ParentID, SectionLevel, SectionTitle) AS
(
SELECT SectionID, ofs.ParentID, ofs.SectionLevel, ofs.SectionTitle
FROM Section ofs
WHERE ofs.ParentID = 0
ORDER BY SectionID
UNION ALL
SELECT SectionID, ofs.ParentID, ofs.SectionLevel, ofs.SectionTitle
FROM Section ofs
JOIN tree ON tree.ID = ofs.ParentID
ORDER BY PageOrder
)
This is deliberately slightly niaive to make it easier on my fingers. It can be tuned if necessary to allow more nodes in the tree to be accomodated.
DECLARE
#maxPageOrder INT,
#maxLevel INT,
#multiplier INT
SELECT
#maxPageOrder = MAX(PageOrder) + 1,
#maxLevel = MAX(SectionLevel)
FROM
Section
SELECT
#multiplier = POWER(#maxPageOrder, #maxLevel - 1)
;
WITH
recursed_tree
AS
(
SELECT
SectionID AS SectionID,
PageOrder * #multiplier AS finalOrder,
#multiplier / #maxPageOrder AS multiplier
FROM
Section
WHERE
ParentID = 0
UNION ALL
SELECT
child.SectionID,
parent.finalOrder + child.PageOrder * parent.multiplier,
parent.multiplier / #maxPageOrder
FROM
recursed_tree AS parent
INNER JOIN
Section AS child
ON child.ParentID = parent.SectionID
)
SELECT
Section.*
FROM
Section
INNER JOIN
recursed_tree
ON Section.SectionID = recursed_tree.SectionID
ORDER BY
recursed_tree.finalOrder
Note: This assumes all page orders start from 1, rather than 0.
Here's the messy solution I was talking about..
select
a.SectionID, a.SectionTitle, a.SectionLevel, a.ParentID, a.PageOrder
from
section a
left outer join section b on b.SectionID=a.ParentID
left outer join section c on c.SectionID=b.ParentID
order by
c.PageOrder, b.PageOrder, a.PageOrder
As I mentioned in the comment, better to do it in code.. this is only for 3 levels (you could make it more by adding another outer join and order by clause).
This is rather complicated in SQL. If you have access to Safari Books, there are some relevant sections in Joe Celko's Trees and Hierarchies in SQL for Smarties.