Here is the situation,
I have an interface with a dropdown(All, Category, Category type, Product) and search button. There are three database tables for each section as CATEGORY, CATEGORYTYPE and PRODUCT
When user select the All option search, I am getting results from all three tables and display on a HTML table.
TO do pagination, I want to fetch results set by set from all the tables.
Here are the three queries I'm using:
SELECT CATNAME,CATDESC,CATSTATUS FROM CATEGORY WHERE CATNAME like %a%
SELECT CATTYPENAME,CATTYPEDESC,CATTYPESTATUS FROM CATEGORYTYPE WHERE CATTYPENAME like %a%
SELECT PRODUCTNAME,PRODUCTDESC,PRODUCTSTATUS FROM PRODUCT WHERE PRODUCTNAME like %a%
From all the tables I'm only fetching the same set of column I want to combine the three queries. I hope my question is understandable. This is not about joining the three tables.. I want the results from all three queries to act as a one set or records and paginate them.
Thanks in advance for any suggestions and please ask if the question is not clear.
The application is JAVA EE based. Struts used and DB is MySQL.
Using view u can do what you want.
in very first u have to change columns heading as same. like
SELECT NAME,DESCRIPTION,STATUS FROM CATEGORY WHERE NAME like %a%
SELECT NAME,DESCRIPTION,STATUS FROM CATEGORYTYPE WHERE NAME like %a%
SELECT NAME,DESCRIPTION,STATUS FROM PRODUCT WHERE NAME like %a%
then you can create view like this
CREATE VIEW V AS
SELECT *
FROM ((CATEGORY NATURAL FULL OUTER JOIN CATEGORYTYPE)
NATURAL FULL OUTER JOIN PRODUCT);
P.S. if you can not change columns heading,you can have to change view according to that tables' columns
Related
I have 2 tables:
acco_info
acco_revenue_2016
Both tables have different columns except for 1, acco_id.
This column is what connects both tables.
I want to write a query that combines important data from both tables and links them to the acco_id.
So from the acco_info table I need the following columns:
acco_id, acco_name, region_name, country_name
From the acco_revenue_2016 table I need:
acco_id, sales, revenue_per_item, revenue
The output should look like this:
acco_id, acco_name, region_name, country_name, sales, revenue_per_item, revenue
What's the best way to write this query?
I am stuck on
SELECT acco_id FROM acco_info UNION SELECT acco_id FROM acco_revenue_2016
This joins the ID's together but I can't find a way to also show the other data.
You'll be looking for something like this;
SELECT
ai.acco_id
,ai.acco_name
,ai.region_name
,ai.country_name
,ar.sales
,ar.revenue_per_item
,ar.revenue
FROM acco_info ai
INNER JOIN acco_revenue_2016 ar
ON ai.acco_id = ar.acco_id
This is assuming that both tables contain the same acco_id. Notice the table aliases to see which field is coming from each table
I am new at Stackoverflow, sorry for my poor english, and please do not give negative marks on my question, I really need ans for this.
I have been working on one assignment. I have two tables sale_head and sale_details
Table sale_head has following fields. -- invoiceno (PK),suppliername,invoicedate
and Table sale_details has following fields. -- invoiceno(FK),suppliername,product_code,qty,totalkg,rate,subtotal
Basically What I have done here is, i have two tables master and slave, master table has all head information of the data and details tables has rest of the details like product code, product quantity, rate, and total etc.
So far I have come up with one SQL query which is below
SELECT sale_head.suppliername AS sale_head_suppliername,
sale_head.invoiceno AS sale_head_invoiceno, sale_head.invoicedate,
sale_details.invoiceno AS sale_details_invoiceno,
sale_details.suppliername AS sale_details_suppliername,
sale_details.product_code, sale_details.qty, sale_details.totalkg,
sale_details.Rate, sale_details.subtotal
FROM sale_head
INNER JOIN sale_details ON sale_head.[invoiceno] = sale_details.[invoiceno]
WHERE (((sale_head.suppliername)='Ramkrishna Creation'));
Above query display some certain result(date have been displayed in high amount so) please refer this image link. which will show you results of above SQL query
The Result I have come up : Image link
What result I want to display is : Image Link
I want Sum of Subtotal at end of the invoice number as I have shown in above result image, Do not get attend on colors, I have just displayed color you draw your attention.
I really need answer for this so help me.
You can do this with UNION ALL as:
SELECT sale_head.suppliername AS sale_head_suppliername,
sale_head.invoiceno AS sale_head_invoiceno, sale_head.invoicedate,
sale_details.invoiceno AS sale_details_invoiceno, sale_details.suppliername AS sale_details_suppliername,
sale_details.product_code, sale_details.qty, sale_details.totalkg, sale_details.Rate, sale_details.subtotal FROM sale_head
INNER JOIN sale_details ON sale_head.[invoiceno] = sale_details.[invoiceno]
UNION ALL
select 'Total', sum(sale_details.subtotal) from sale_details
WHERE (((sale_head.suppliername)='Ramkrishna Creation'));
I am trying to create a table that shows treatment information about patients (though I just wondered if would be better as a query) at a fictional hospital. The idea is that one row of this could be used to print an information sheet for the attending nurse(s).
I would like to make the attending_doctor column contain the name that corresponds with the employee_id.
|Patient_ID|Employee_ID|Attending_Doctor|Condition|Treatment|future_surgery|
Would appreciate any help. Thank you!
Just use a join in your query rather than have the employee name in 2 tables (which would mean updating in more than one location if they change name etc). For the sake of an example, this also gets the patients name from a 3rd table named patients.
eg
SELECT table1.*, employees.name, patients.name
FROM table1
LEFT JOIN employees ON employees.id = table1.employeeId
LEFT JOIN patients ON patients.id = table1.patientsId
Don't use directly this table, but build a view that contains the data you need. Then you can get the data from the view like it was a table.
Basically what you need is to have data in three tables. One table for patients, one table for for employees and one for the reports. Table with reports should contain only the employee_ID. Then you can either build a direct query over these three tables or build a view that will hide the complicated query.
I have 2 tables in a MySQL database: Products and ProductsType.
Each element of the Products table has a type which is contained in ProductsType table.
Each table has a field named "code".
I'd like to have a VIEW with the cartesian product of these 2 tables and I know that I can do it with a CROSS JOIN.
But I'd like that in this VIEW I can see the CONCAT of the 2 fields named "code" of my 2 tables. Is that possible?
I've also thought that I can make 3 tables, Products, ProductsType and ProductsWithType and in the first 2 I put some triggers "after insert" that keep updated the third table ProductsWithType, but I'd like to know if it is possibile to do with a view.
Thanks in advance
Are you looking for this?
CREATE VIEW vw_products_types AS
SELECT CONCAT(p.code, t.code) combined_code, p.product_name, t.type_name
FROM Products p CROSS JOIN ProductsType t
Here is SQLFiddle demo
I wanted to know if it was possible to make a select on a table that contains multiple field and join them in 1 result :
Example :
Table :
id
dayOne_City
dayTwo_City
dayThree_City
Result : one column that contains the rows of all the cities (Distinct).
2) Am i better to do a view if i have a lot of query to that specific list ?
3) should i do 3 select with union ?
Thank you
You should be fine with:
select dayOne_City from YourTable
UNION
select dayTwo_City from YourTable
UNION
select dayThree_City from YourTable
However, you should review your design to allow multiple cities per whatever-is-that-your-table stores. That is, create an actual many-to-many relationship by creating an intermediate table between YourTable and Cities.
select concat_ws(',', id, dayOne_city, dayTwo_city, dayThree_city, etc...) as allInOne
Details on the function used here. However, I should ask why you're doing this. By joining the fields together, you're destroy any chance of reliably extracting/separating the data again later. Only do this kind of "bulking" if you never plan on using separate portions of the data elsewhere based on the results of this query.