I have sql like:
select employee_name, employee_id from employees;
select admin_name, admin_id from admin;
Yep! we all know how to populate an sql query in a dropdown list but what I am looking for is on how would I possibly populate the data of this two sql in a single dropdown ?
Does anyone have a magical idea ? BTw, I'm using c# asp.net and even open to use jquery, json or the like to achieve this.
You can use JOIN to combine these 2 queries.The number of columns and datatype of corresponding columns in each queries must be same.
select employee_name as name, employee_id as id from employees
UNION
select admin_name as name, admin_id as id from admin
you can use 'name' as textfield and 'id' as value field
If you are trying to create dropdown data using this query,there may be a problem of identifying admin and employees seperately(both may have same id)
you can try this
To get both table data in single query, you should put union between both tables. Column names from both tables should same also. For that you can use column aliasing.
(select employee_name as name, employee_id as id from employees)
Union all
(select admin_name as name, admin_id as id from admin);
You can bind data from this query to your drop down list.
Hope it work for you.
Related
I have 3 tables (for example 3, but in real over than 30 tables with this conditions) in my SQL Server database: post, user, person.
post: (post_id, post_text, user_id)
user: (user_id, user_name, person_id)
person: (person_id, person_phone, person_email)
Now, in C#, I want an algorithm that creates a query that get result like this:
post.post_id, post.post_text, post.user_id, user.user_id, user.user_name, user.person_id, person.person_id, person.person_email
and I use this method for fill a SqlDataReader in C# for reading and accessing all column values from these records.
I know that the common way to get that result directly and manually using of 'Join' statement, but it is waste time if tables count is very much. So, I want an algorithm that generates this query for manipulate in C# programming.
Thanks a lot.
To query all column names from your tables, you can use:
SELECT obj.Name + '.' + col.Name AS name
FROM sys.columns col
INNER JOIN sys.objects obj
ON obj.object_id = col.object_id
WHERE obj.Name IN ('post', 'user', 'person')
ORDER BY name
Then, for how to call this from C# using SqlDataReader, you can use this documentation: https://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.110).aspx
select post1.post_id, post1.post_text, post1.user_id, user1.user_id, user1.user_name, user1.person_id, person1.person_id, person1.person_email from post post1 inner join user user1 on user1.user_id=post1.user_id inner join person person1 on person1.person_id=user1.person_id
I have two tables..
Students
StudentID
Name
SchoolID
School
SchoolID
SchoolName.
This is my scenario, I have created a DetailsView on my page which displays X StudentID and its columns. When displaying the SchoolID (from Students table) column it will display the actual ID as its supposed to.
I would like to know how I can display the actual school name instead of the schoolID, what I did first was simply have a query inside the first query which displays the name of the school like...
SELECT StudentID, (SELECT SchoolName FROM Schools
WHERE Schools.SchoolID = Students.SchoolID)
FROM Students WHERE StudentID=1
Although this works this creates issues when trying to edit the record via GridView, it will not save the correct data in those fields.
In the edit step in GridView I'm able to bind the SchoolID key with SchoolName, however this will NOT work when having a query within a query.
So my question really is how can I display foreign key data within Visual Studio without changing the SQL command?
The solution previously was to edit the Select command (after the SQLDatasource was created) to perform a inner join on the table containing the meaningful field name.
Check Here
1.Link
2.Link
You could do that with join query, example:
select students.StudentId,students.Name,students.SchoolID,school.SchoolName from students join school on students.SchoolID=school.SchoolID
And, for better understanding of "JOINS" here is useful link: http://www.sitepoint.com/understanding-sql-joins-mysql-database/
I want to compare two tables with the same columns:
product - Id, Name, Description
Temp_Product - Id, Name, Description
Now update done by user will be saved into Temp_Product. When admin will see the details of that product I need to show the changes done by user. I want to compare both tables with a query and return columns that have changed from Product to Temp_Product.
Please suggest me better way to do this?
Select p.id,p.name as orgn,t.name as altn,p.descripion as orgd,t.description as altd
from product p
join tmp_product t
on t.id=p.id and (t.name<>p.name or t.description <> p.description)
I want to compare both tables with a query and return columns that
have changed from Product to Temp_Product
Since the two tables have the same structure, you can use the EXCEPT set oeprator for this:
SELECT * FROM Temp_Product
EXCEPT
SELECT * FROM Product;
SQL Fiddle Demo
I have a local report that I created in my application. It displays the contents of a table in my access database called "History". One of the columns is "ID" and it refers to the primary key in another table "Employees".
My report shows the employee's ID when it is refreshed but of course I want it to display the employee's name instead.
In short, I need to be able to perform a lookup on another table in my rldc file. How can I go about that?
I assume you fetch data from the database through a dataadapter and a typed dataset. something like this?
TestDataSetTableAdapters.CategoryTableAdapter ca = new TestDataSetTableAdapters.CategoryTableAdapter();
this.ds1 = new TestDataSet();
ca.Fill(this.ds1.Category);
Then you go to the dataset and modify the query in your tableadapter to something like this
select h.*,e.EmployeeName from history h
inner join Employees e on e.ID = h.UserID
Then the datset will be modified to include the column EmployeeName on all rows in the History table. Then the column "employeename" is directly available in yyour report
Here is an example of how to join your employee and history tables with SQL. My Access is rusty, so the syntax might not be perfect, but this should demonstrate the concept. Obviously, I made up the column names, so you will have to change the columns to whatever you need.
SELECT EmployeeID,
EmployeeName,
TimeIn,
TimeOut
FROM Employee Emp
INNER JOIN History History
ON Emp.EmployeeID = History.EmployeeID
I am new to ADO.net and have this problem:
Let's assume I have these two tables in a SQL Server 2005 database, with these columns:
[Orders]
OrderID
OrderDate
ShopID
TotalAmount
TotalTaxAmount
etc...
[OrdersDetails]
OrderID
ShopID
ItemID
Quantity
Amount
TaxAmount
etc
I have started a WinForms application to get myself started.
In this form, the user can select a list of Shops and select a date range to see all orders from this shop.
I have added a data source from Visual Studio, select both Orders and OrdersDetails table and dragged and dropped the Orders and related OrdersDetails tables into the form as DataGridViews.
When I select a row from the Orders DataGridView, I indeed see the corresponding Orders Details in the second DataGridView as I wanted. I had relationships inside this database and ADO.net caught them up and reflected them in the dataset.
I have then added a method to my typed dataset to get data by the OrderDate, and ShopID column.
As the OrdersDetails table does not have an OrderDate column, I could only filter it by ShopID.
The issue is that it is time consuming to get the records from the OrdersDetails as it will retrieve more rows than needed into the DataTable for the OrdersDetails.
The problem is that I can only filter the rows from the OrderDetails table by ShopID, which returns way too many records from the database.
Obviously, ADO.net is able to filter them appropriately on the client-side by using the OrderID relationship but it would make much more sense to retrieve only the rows from the OrdersDetails that I actually need.
I have modified my queries getting the data from the second table to add the OrderDate using a join, so I can filter by date when I retrieve the data from the database... however, it causes problems when I try to update my changes due to this foreign column...
I believe there must be an easy way around this, isn't there?
Thanks a lot in advance.
You want to do something like this
SELECT *
FROM OrderDetails
WHERE
ShopID IN ( #listOfShopIds )
AND
OrderID IN (
SELECT OrderID
FROM Orders
WHERE
OrderDate BETWEEN #dateFrom AND #dateTo
)
#David it's probably better to write code which expresses your intent and reflects the more performant algorithm, rather than relying on implementation details of the engine to perform the optimization.