Sub query not working correctly - mysql

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);

Related

MYSQL, Creating a view and pulling information from two tables

Okay so I have two tables:
hscust and hssales_rep
I need to create a view that shows me the reps fname and lname (as well as the customers) and show how much the customer is over on there credit balance.
This is the code I have:
CREATE VIEW OverLimit AS
SELECT
CONCAT(hssales_rep.last,hssales_rep.first) AS Rep,
CONCAT(hscust.last,hscust.first) AS Cust,
SUM(credit_limit - balance)
FROM hscust
INNER JOIN hssales_rep ON hscust.sales_rep = hssales_rep.repid
And it returns an empty result.
Any help is greatly appreciated!
salesrep table
cust table
A CREATE VIEW statement doesn't return a resultset.
A SELECT statement can return an empty resultset. But we'd expect the SELECT statement in your view definition to return either a single row, or throw an error.
I suggest you break this down a bit.
1) What problem is being solved by the CREATE VIEW statement. Why do you need a view?
2) Before you write a CREATE VIEW statement, first develop and test a SELECT statement that returns the required resultset. Do that before you put that into a view definition.
I also strongly recommend that you qualify all column references in the SELECT statement either with the table name or (preferably) a short table alias.
If you want to return a row for each Cust with an aggregate function (e.g. SUM) in your SELECT list, then add an appropriate GROUP BY clause to your SELECT statement.
It's not clear why we would want to use a SUM aggregate function.
The difference between "credit_limit" and "balance" would be the available (remaining) credit. A negative value would indicate the balance was "over" the credit limit.
SELECT CONCAT(r.last,r.first) AS Rep
, CONCAT(c.last,c.first) AS Cust
, c.credit_limit - c.balance AS available_credit
FROM hscust c
JOIN hssales_rep r
ON c.sales_rep=r.repid
ORDER
BY CONCAT(r.last,r.first)
, CONCAT(c.last,c.first)
, c.custid
If we only want to return rows for customers that are "over" their credit limit, we can add a WHERE clause.
SELECT CONCAT(r.last,r.first) AS Rep
, CONCAT(c.last,c.first) AS Cust
, c.credit_limit - c.balance AS available_credit
FROM hscust c
JOIN hssales_rep r
ON c.sales_rep=r.repid
WHERE c.credit_limit - c.balance < 0
ORDER
BY CONCAT(r.last,r.first)
, CONCAT(c.last,c.first)
, c.custid
Again, get a SELECT statement working (returning the required resultset) before you wrap it in a CREATE VIEW.

SQL statement - compare two tables columns

I'm new to PHP and SQL.
I want to compare two table-columns.
This is what we have
Table 1(Review)
Table 2(Customer)
We want the Review_Customer_Id to be = to the Customer_Id in the Customer Table
How would you write this?
In my head it would be something like this:
SELECT * FROM Customer
WHERE Customer_Id = Review_Customer_Id IN (SELECT Review_Customer_Id FROM Review);
Check this Answer
SELECT (fistnameField + "" + LastnameField) AS Name FROM Customer WHERE Customer_Id IN (SELECT Customer_Review_Id FROM Review);

how to get number only from string field mysql

I have field that has value kind of store878 . I would like to have 878 from select statement. How do I get that numbers from select statement
select
store,
address
from
detail,
store_number
where
store (here i would like to have number) = store_mumber.id
I haven't tested this to make sure is works:
SELECT store, address
FROM detail d
INNER JOIN store_number s
ON CAST(SUBSTRING(d.store, LOCATE('%[0-9]%', d.store)) AS int) = s.id
But you should really consider changing the structure of your database.

multiple query with sqlite 2nd query gives only first result

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

Dynamic query string

I want to add some dynamic content in from clause based on one particular column value.
is it possible?
For Example,
SELECT BILL.BILL_NO AS BILLNO,
IF(BILL.PATIENT_ID IS NULL,"CUS.CUSTOMERNAME AS NAME","PAT.PATIENTNAME AS NAME")
FROM
BILL_PATIENT_BILL AS BILL
LEFT JOIN IF(BILL.PATIENT_ID IS NULL," RT_TICKET_CUSTOMER AS CUS ON BILL.CUSTOMER_ID=CUS.ID"," RT_TICKET_PATIENT AS PAT ON BILL.PATIENT_ID=PAT.ID")
But This query is not working.
Here
BILL_PATIENT_BILL table is a common table.
It can have either PATIENT_ID or CUSTOMER_ID. If a particular record has PATIENT_ID i want PATIENTNAME in RT_TICKET_PATIENT as NAME OtherWise it will hold CUSTOMER_ID. If it is i want CUSTOMERNAME as NAME.
Here I m sure That BILL_PATIENT_BILL must have either PATIENT_ID or CUSTOMER_ID.
Can anyone help me?
You can also use IF() to select the right values instead of constructing your query from strings:
SELECT
BILL.BILL_NO AS BILLNO,
IF( BILL.PATIENT_ID IS NULL, cus.CUSTOMERNAME, pat.PATIENTNAME ) AS NAME
FROM
BILL_PATIENT_BILL AS BILL
LEFT JOIN RT_TICKET_CUSTOMER cus ON BILL.CUSTOMER_ID = cus.ID
LEFT JOIN RT_TICKET_PATIENT pat ON BILL.PATIENT_ID = pat.ID
However, it would also be possible to PREPARE a statement from strings and EXECUTE it but this technique is prone to SQL injections, i can only disadvise to do so:
read here: Is it possible to execute a string in MySQL?