I have two very simple questions:
what is name of a select statement result table in MySQL (a name that I can use in another select statement, for example in next line)?
how to naming that above table (in question 1)?
1- what is name of a select statement result table of in mysql?(a name that I can use it in another select statement(for example in next line))
Resultsets will not have any name unless you define an alias name while executing.
select * from department dept
In the above example dept is an alias name for department table.
2- how to naming that above table(in question 1)?
There could be cases where you select information from a single or multiple tables and join together. For that whole set you can define an alias.
select
emp_id, salary, commission,
( salary + commission ) as total_sal,
dept.dept_name as department_name
from employee emp, department dept
where emp.dept_no = dept.dept_no
For the above query you can give alias when used like:
select * from (
select
emp_id, salary, commission,
( salary + commission ) as total_sal,
dept.dept_name as department_name
from employee emp, department dept
where emp.dept_no = dept.dept_no
) as filtered_results
where
department_name in ( 'sales', 'marketing' )
Refer to:
MySQL: Schema Object Names
If I am correct, you wish to create a from a select command in mysql? This should work.
CREATE TABLE fishTypes (type VARCHAR(100)) SELECT type FROM allFish;
Is this the solution you are looking for? Here allFish might have the schema
(number INT, type VARCHAR(100))
it was found. I must use views.
Related
I have a table in power bi which uses the below dax:
need inputs on SQL Query.
powerbi table name: demo,
employee is another table and demo table uses it and KEY IS ALREADY CREATED IN EMPLOYEE TABLE BY CONCATINATING employee'[EMP_CODE]&'employee'[dob]
key=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
WORKEDHOURS = CALCULATE(MAX('Employee'[workhrs]), 'Empployee'[NAME]<>BLANK())
employeecode=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
employeedob=SUMMARIZE( FILTER('employee','employee'[emp_STS]="working"),'employee'[EMP_CODE],'employee'[dob],"Key",'employee'[EMP_CODE]&'employee'[dob])
corresponding SQL Query I have tried :
select KEY,
CASE
WHEN NAME IS NOT NULL THEN MAX(HOURS)
END AS WORKEDHOURS,
EMP_CODE,
EMP_DOB
from employee where EMP_STS='WORKING' GROUP BY KEY,QC_NAME,DOB,EMP_CODE
Please, check this part and come with your feedback.
WITH
e1 AS (
SELECT
CONCAT([EMP_CODE],[dob]) AS [Key]
,EMP_CODE
,EMP_DOB
FROM employee
WHERE [emp_STS]='working'
GROUP BY [EMP_CODE],[dob]
)
,e2 AS(
SELECT
CONCAT([EMP_CODE],[dob]) AS [Key]
,MAX([workhrs]) AS [WorkedHours]
FROM employee
WHERE [NAME] IS NOT NULL -- ,[emp_STS]='working' ?
GROUP BY [EMP_CODE] ,[dob]
)
SELECT
e1.[Key]
,e1.[EMP_CODE]
,e1.[EMP_DOB]
,e2.[WorkedHours]
FROM e1
LEFT JOIN e2
ON e1.[Key]=e2.[Key]
Given two tables containing path columns that indicate the hierarchical placement in an organization, and emp1 that is a member of team1 in department1 in company acme (an emp can have multiple paths), and department managers (one manager can manage several departments):
create table emp (
username varchar(10),
path varchar(255)
);
create table manages (
username varchar(10),
path varchar(255)
);
insert into emp (username, path)
values ('emp1', '/acme/dept1/team1'),
('emp1', '/acme/dept9'),
('emp2', '/acme/dept2/team1');
insert into manages (username, path)
values ('mngr1', '/acme/dept1'),
('mngr2', '/acme/dept2'),
('mngr2', '/acme/dept3');
How can I check if emp1 is a subordinate of mngr1, i.e. if there is a manages.path that is a prefix of emp.path?
Something like:
select manages.username
from emp
join manages on manages.path is-prefix-of emp.path
where emp.username = 'emp1' and manages.username = 'mngr1'
which should return mngr1.
The actual path implementation is based on a fixed length primary key encoding (meaning that by construction /acme/dept1 wouldn't be a prefix of /acme/dept10).
Use a LIKE expression here:
SELECT m.username
FROM emp e
INNER JOIN manages m
ON e.path LIKE CONCAT(m.path, '%')
WHERE e.username = 'emp1' AND m.username = 'mngr1';
The LIKE expression in the above query says that e.path starts with m.path.
I am currently creating a database and trying to use a variety of queries. I can't however seem to get my sub query to work.
The code below shows my current code I'm trying to get the customers name, address, and telephone when the customer ID is = 21345432 instead the display shows me all customers not just that specific one.
Code:
select Customer_Name, Customer_Address, Customer_Telephone
from CustomerInfo
where (Select CustomerID from CustomerInfo where CustomerID = "21345432");
If you did need a sub-query it would look like this:
select Customer_Name, Customer_Address, Customer_Telephone
from CustomerInfo
where specialID = (Select specialID from sometable);
Remember, if the sub-query gives more than one result this will give an error. If you want more than one result then you really want a join. (or use in like Prdp's answer)
You don't need sub-query
SELECT Customer_Name,
Customer_Address,
Customer_Telephone
FROM CustomerInfo
WHERE CustomerID = 21345432
Double quotes are used for identifiers.For string literals use single quotes and Integer values can be added directly
Update: (Just an example to use sub-query)
SELECT Customer_Name,
Customer_Address,
Customer_Telephone
FROM CustomerInfo
WHERE CustomerID in (SELECT CustomerID
FROM CustomerInfo
WHERE CustomerID = 21345432);
SELECT quantity, materialTypeId ,
(SELECT typeName
FROM invTypes
WHERE TypeID IN (SELECT materialTypeId
FROM invTypeMaterials
WHERE typeId= 12743
)
) AS material
FROM invTypeMaterials
WHERE TypeID=12743
so this query gives me nice results except the column material. only shows me the first entry instead of giving the name of each row.
if i run these sql seperate they work and i do see what i want. i just need them combined into 2 columns.
what i want to do is, i query one table for data, one of the column has a value wich i want to convert to a name, and that is in another table and its linked by a unique TypeID
Chilly
May be this will work :
SELECT tm.quantity, tm.materialTypeId , t.typeName
FROM invTypeMaterials tm
INNER JOIN invTypes t ON t.TypeID = tm.materialTypeId
WHERE tm.TypeID=12743
If you want to lookup the materialTypeID's name for the current record, you must not use a separate subquery but use the materialTypeID value from the outer query.
This is called a correlated subquery:
SELECT quantity, materialTypeId,
(SELECT typeName
FROM invTypes
WHERE TypeID = invTypeMaterials.materialTypeId
) AS material
FROM invTypeMaterials
WHERE TypeID=12743
I write a query to get values from 3 tables but this is returning multiple values so can any one tell where i went wrong
select c.CompanyName,cd.FedTaxID,cd.EmailAddress,cd.PhoneNumber
from tblcustomerdetail cd,tblcustomer c
where c.FedTaxID in (
select FedTaxID
from tblcustomer
where CustomerID in (
select LOginID
from tbluserlogindetail
where UserName like "pa%" and RoleTypeID='20'
)
)
and cd.FedTaxID in (
select FedTaxID
from tblcustomer
where CustomerID in (
select LOginID
from tbluserlogindetail
where UserName like "pa%" and RoleTypeID='20'
)
);
My relation is here
My 3 tables are `tbluserlogindetails, tblcustomerdetails and tblCustomer'
1) Initially i will get `Login ID` from `tblUserLoginDetail ` based on the `user name`.
2) Next based on `LoginID` i will get `FedTaxID` from tblcustomerDetail`
3) Next based on 'FedTaxID' i will get the the required details from `tblcustomer'
SELECT
tblcustomer.CompanyName,
tblcustomerdetail.FedTaxID,
tblcustomerdetail.EmailAddress,
tblcustomerdetail.PhoneNumber
FROM tbluserlogindetail, tblcustomer, tblcustomerdetail
WHERE
tbluserlogindetail.LOginID = tblcustomer.CustomerID
AND tblcustomer.FedTaxID = tblcustomerdetail.FedTaxID
AND tbluserlogindetail.UserName LIKE 'pa%'
AND tbluserlogindetail.RoleTypeID = '20'
Try something like this.
Subqueries have a slow perfomance.
MySQL - SELECT WHERE field IN (subquery) - Extremely slow why?