hi I'm just starting to learn sql and I wish to combine the columns into one using concat but didn't manage to do it. I can run the code without concat but when when I use concat it gives me an error code. Can anyone tell me what am I doing wrong?
SELECT CONCAT('A purchase with the purchase ID of' AS "Constraint",
ONLINEPURCHASE.PurchaseID AS "OLID", 'is an online purchase of type' AS "Condition", ONLINEPURCHASE.OnlineType AS "OLType", 'and also a walkin purchase of location' AS "Condition", WALKINPURCHASE.ShopLocation AS "ShopLocation")
FROM ONLINEPURCHASE JOIN WALKINPURCHASE
ON ONLINEPURCHASE.PurchaseID = WALKINPURCHASE.PurchaseID
WHERE WALKINPURCHASE.PurchaseID IN (SELECT PurchaseID
FROM WALKINPURCHASE);
But got this error (ERROR 1583 (42000): Incorrect parameters in the call to native function 'concat')
Congratulations on starting to learn SQL, it's super useful! Let me try to clear up your misunderstanding.
The CONCAT function takes some number of either strings or column values and joins them together. There's no need to use the AS keyword within it.
Here's what I think you're looking for:
SELECT
CONCAT(
'A purchase with the purchase ID of',
ONLINEPURCHASE.PurchaseID,
'is an online purchase of type',
ONLINEPURCHASE.OnlineType,
'and also a walkin purchase of location',
WALKINPURCHASE.ShopLocation
) AS result
FROM ONLINEPURCHASE JOIN WALKINPURCHASE
ON ONLINEPURCHASE.PurchaseID = WALKINPURCHASE.PurchaseID
WHERE WALKINPURCHASE.PurchaseID IN
(SELECT PurchaseID FROM WALKINPURCHASE);
This will create the sentence you're building in the CONCAT statement and return it as a single column of your result set. (I've used the AS keyword to rename the column you've built.)
Please let me know if you have any questions about this.
Related
def fetchProposalByStudio(studioId: Int): List[ProposalDetails] = {
ConnectionPoolManager.getDB(config = appConfig).localTx { implicit session: DBSession =>
logger.info("Querying proposal table to fetch all the proposals")
SQL("""SELECT [except name] p.id, id, proposal_title, challenge, possible_solution, explanation,
| submission_date, status, submitted_by, remark
| FROM proposal p inner join knolder k on k.id = p.knolder_id
| where k.studio_id =? order by p.id desc""".stripMargin)
.bind(studioId)
.map(rs =>
ProposalDetails(
rs.int("id"),
rs.int("id"),
rs.string("proposal_title"),
rs.string("challenge"),
rs.string("possible_solution"),
rs.string("explanation"),
rs.string("submission_date"),
Some(ProposalStatus.withName(rs.string("status"))),
rs.string("submitted_by"),
rs.string("remark"),
**rs.string("name")**
)
)
.list().apply()
}
}
I don't want to fetch this column name in my query but without involving this in the query i am getting this error due to using case class.
13:28:24.446 [default-akka.actor.default-dispatcher-8] INFO c.k.l.b.ProposalImpl - Something went wrong while fetching the proposals. Exception message: ERROR: syntax error at or near "["
Position: 8
Smells of a syntax problem...
Perhaps:
SELECT [except name] -- should be
SELECT `except name` -- in mysql
If you don't want a particular column in an SQL resultset, you simply don't mention it in the SELECT.
There is no notion of SELECT * EXCEPT FirstName FROM person - if Person has FirstName, LastName, Age, Address and you don't want FirstName, you don't put it in the select list:
SELECT LastName, Age, Address FROM Person
^^^^^^^
no FirstName mentioned here
Mention every column you do want, do not mention any column you don't want.
If the complaint is "but there are 527 columns and I want all except one" - you can do something like:
SELECT CONCAT(column_name, ',') FROM information_schema.columns WHERE table_name = 'Person' and column_name <> 'FirstName'
which produces a resultset like:
LastName,
Age,
Address,
... 523 other columns
And you can then copy that resultset and paste it into your code, and it already has commas on the end..
If you want the columns all on one line, use GROUP_CONCAT or use a decent text editor to replace \r\n with nothing. If you want to surround the column name in backticks, put it into the CONCAT.. The ultimate point here is that you're a software developer: you can write code that writes code, then you can copy the output, which is valid code, and paste it into some other code somewhere else
BOOK_ADOPTION (course:int, sem:int, book_isbn:int)
TEXT (book_isbn:int, booktitle: varchar(50), publisher: varchar(50), author: varchar(50))
List the books which are adopted by the course as well as enrolled by the student.
I am trying this:
select course
from text
natural join book_adoption
group by course
having count(book_isbn)>1
and publisher = 'perason';
and getting an error:
Unknown column 'publisher' in 'having clause'
but the column publisher is there and even the spelling is correct.
Can someone help me out with this one?
HAVING clause is for using with aggregating functions (like count) but in the case of publisher you have to simply use the WHERE condition.
select course
from text natural join book_adoption
where publisher = 'perason'
group by course
having count(book_isbn)>1;
The difficult thing with this question is the fact that there is no real date and I am supposed to extract the date from a description like this "1960 Ford".
I have tried using a sub-string but I was not successful.
this is the query that I used
Select productlines
from products
where productsline= "Classic cars"
and substring('productname', 1, 4) =>1960 and substring('productname', 1, 4)>=1970
Please, try with below SQL Query:
Select productlines
from products
where productsline= "Classic cars"
and CAST(substring('productname', 1, 4) as SIGNED) =>1960 and CAST(substring('productname', 1, 4) as SIGNED)>=1970
You have done correct query but just you need to remove ' sign from sub string function because column name should not define in string.
Let me know if you still face difficulty.
I'm trying to do an assignment for my course where one of the questions is to get the faculty name, date hired (in dd-mon-yyyy format) and that have the same title as Erit Jackson in the database, who's title is Assoc Prof. My code looks identical to the example provided by the instructor, but when I run this code, I get the error:
2014 - Commands out of sync; you can't run this command now
I don't know what I'm doing wrong thus far?
SELECT CONCAT('Dr.', ' ', LName) As 'Faculty Name', DateHired
FROM Faculty
WHERE Title = (
SELECT Title
FROM Faculty
WHERE Title = 'Assoc Prof');
This doesn't make a lot of sense as written. It can be simplified:
SELECT CONCAT('Dr.', ' ', f1.LName) As 'Faculty Name', f1.DateHired
FROM Faculty f1
INNER JOIN Faculty f2
ON f1.Title = f2.Title
WHERE f2.FName = 'Erit'
AND f2.LName = 'Jackson';
The way you are using the subquery is redundant. You're asking the database for the person that has the title(select & from), that matches(where title =) a table of titles with the title of 'Assoc Prof'(subquery).
Your professor wants your subquery to find the title of Erit Jackson, not to specifically find the title "Assoc Prof". Do you see the difference? You won't ever have the text "Assoc Prof" in your query at all.
From PHP Code $Lines is defined as a list of accessions e.g. 123,146,165,1546,455,155
plant table has sequential records with the highest idPlant (unique identifier) of say 1000.
My simple SQL Query:
SELECT * FROM plant WHERE `plant`.idPlant IN($Lines) order by plant.idPlant;
This brings back row data for '123,146,165' etc.
Is there away to be told that '1546' was not found? (and thus the user probably entered a typo, I can not use a 'confirm all numbers are below X' because in the real data the idPlant may not be sequential and the upper bound will increase during use).
Update:
Looking to get an output that will tell me what Numbers were not found.
You can build up a sub query using unions that returns a list of all your values, then LEFT JOIN against that, checking for NULL in the WHERE clause to find the non matching values.
Basic php for this would be something like this:-
<?php
$sub_array = explode(',', $Lines);
$sub = '(SELECT '.implode(' AS i UNION SELECT ', $sub_array).' AS i) sub0';
$sql = "SELECT sub0.i
FROM $sub
LEFT OUTER JOIN plant
ON plant.idPlant = sub0.i
WHERE plant.idPlant IS NULL";
?>
You can create a temporary table and compare it to the original table. It goes something like this:
CREATE TEMPORARY TABLE IF NOT EXISTS plantIDs (
ID INT(11) NOT NULL UNIQUE,
found INT(11) NOT NULL);
INSERT INTO plantIDs(ID) VALUES (123),(146),(165),(1546),(455),(155);
SELECT plantIDs.ID, COALESCE(plant.name, "Not Found") as PlantName, plant.* FROM plant RIGHT JOIN plantIDs ON plant.idPlant=plantIDs.ID ORDER BY plantIDs.ID;
Assuming you have a field named name inside the table plant, this code will produce a row for each plant and the column named PlantName will contain the name of hte plant or the text "Not Found", ofc you can change the coalesce value to anything that fits your needs.