how to write this query in correct syntax? - mysql

SELECT collegename(SELECT allotement.collegename,dean.id
FROM dean,allotement
WHERE allotement.city=dean.city
&&dean.collegename<>allotement.collegename
&&dean.id<>allotement.id)as t WHERE id=1

SELECT collegename from (
SELECT allotement.collegename, dean.id
FROM dean,allotement WHERE allotement.city=dean.city
and dean.collegename<>allotement.collegename
and dean.id<>allotement.id)
as t WHERE id=1
A few points to note here:
Treat sub-query as a table source from which you are retrieving the data. Thus, you need a from in the first line.
&& doesn't work in SQL. You have to write and instead.
In your case, writing as t is optional.
You can actually go through a pretty good link which I generally use to follow mySQL syntax, as it's a bit confusing, considering the fact that different SQL databases have a slight variation in syntax and functions available.
You can refer to the official mySQL docs here as well, if in case required.

TRY THIS: We can simply achieve that in following simple way even we don't need sub query for that:
SELECT a.collegename, d.id
FROM dean AS d
INNER JOIN allotement AS a ON a.city = d.city
AND d.collegename <> a.collegename
AND d.id <> a.id
WHERE d.id = 1

Related

DELETE FROM table WHERE condition is met in other table

I have this SQL query that has been working great. I would like to have something similar that would delete a line from PRC_FIX when the column DESCR in IM_ITEM begins with Clearance instead of where ITEM_VEND_NO = 'GAMES WORK'.
DELETE `PRC_FIX` FROM `PRC_FIX`
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO`
AND `IM_ITEM`.`ITEM_VEND_NO` = 'GAMES WORK'
Thanks for your help.
Edit: This was marked as a possible duplicate. I don't know that looking at the suggested duplicate would have helped me because I wouldn't have known how to implement it in this scenario involving 2 tables, but I'm willing to admit that might be my fault due to me being new to SQL.
You can use
DELETE PRC_FIX
FROM PRC_FIX
INNER JOIN IM_ITEM
ON IM_ITEM.ITEM_NO = PRC_FIX.ITEM_NO
WHERE UPPER(IM_ITEM.DESCR) LIKE 'CLEARANCE%';
You need to use the wildcard %.
in order to match with this string with different string which begins with "Clearance" you need to use "Clearance%".
Look here: SQL like search string starts with
You're fixed code:
DELETE `PRC_FIX` FROM `PRC_FIX`
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO`
AND IM_ITEM.DESCR LIKE 'Clearance%'
DETELE FROM PRC_FIX WHERE ITEM_NO IN (SELECT ITEM_NO FROM IM_ITEM WHERE ITEM_VEND_NO` = 'GAMES WORK')

How to use the select AND

I have been trying to use AND in my SELECT, but I got an error.
What is likely the correct way I should have written the code?
The code is:
$sel=mysql_query("SELECT * from student, subject, studentmark
where student.username='$name' AND studentmark.student_id='$name' AND studentmark.YEAR='$ya' AND studentmark.Term='FIRST' AND studentmark.Term='SECOND' AND studentmark.Term='THIRD' AND subject.code=studentmark.code AND student.username=studentmark.student_id");
A simple rule: Never use commas in the FROM clause. Always use explicit JOIN syntax. The result is something like this:
SELECT *
from studentmark sm join
student st
on sm.student_id = st.username join
subject join
on sm.code = su.code
where st.username='$name' AND sm.YEAR = '$ya' AND
sm.Term in ('FIRST', 'SECOND', 'THIRD')
Also, learn to use table aliases. They make queries easier to write and to read.
You must use OR instead of AND
studentmark.Term='FIRST' AND studentmark.Term='SECOND'
The Value of Term can only has ONE Value
I don't know what columns do you want to get from student, subject & studentmark tables & also relationship of those tables. So here is my idea:
Use OR instead of AND on the same column check: studentmark.Term='FIRST' AND studentmark.Term='SECOND' AND studentmark.Term='THIRD'. This will return nothing.
Use table join query instead of: subject.code=studentmark.code AND student.username=studentmark.student_id, it is not correct. Take a look here: SQL Joins

SQL Syntax with regards to SELECT

I keep getting an error saying that a specific column, doesn't exist for b.BookCode But I am fully aware that it does exist! I just wanted to make sure after staring at this for so long that I am not missing something obvious.
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM (Inventory i, Author a, Book b)
WHERE (i.BookCode = b.BookCode AND b.AuthorNum = a.AuthorNum);
I'm very new to SQL so I'm unsure if my syntax is off, Also do I need parentheses around the columns that I mentioned in SELECT. I had parentheses around them at first and got an error and was confused as to why.
Thanks!
You are using the old join syntax, you should use INNER JOIN instead.
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i
INNER JOIN Author a
USING (BookCode) -- You can also use ON, but USING remove the ambiguous columns
INNER JOIN Book b
USING (AuthorNum); -- Same thing here
If you get an error saying one column doesn't exist, well, you should double check it. MySQL isn't lying to you for the sake of it! You might do your query on an outdated database, for instance.
Minimally, you should change this to:
SELECT AuthorLast, AuthorFirst, OnHand, Title
FROM Inventory i, Author a, Book b
WHERE i.BookCode = b.BookCode AND
b.AuthorNum = a.AuthorNum;
... and this is assuming that the columns in the SELECT part of your select statement are not ambiguous (i.e., found in more than one of the tables in the FROM part).

subsonic 2 join on multiple columns

I want to transfer the following statement to SubSonic 2.2
SELECT b.*
FROM tableA a
INNER JOIN tableB b
ON (a.year = b.year AND a.month = b.monath AND a.userid = b.userid);
My problem is that SubSonic's SqlQuery.LeftInnerJoin() command has no overload which takes more than one column.
Since any join can be rewritten only using where clauses, I did the following in my sql:
SELECT b.*
FROM tableA a, tableB b
WHERE a.year = b.year
AND a.month = b.month
AND a.userid = b.userid
which should deliver the same result (in fact, at least for mysql, there is logically absolutely no difference between these statements).
But I also got stuck transfering this to subsonic because the "IsEqualTo(...)" member is smart enough to figure out that my parameter is a string and puts it into quotes.
DB.Select("TableB.*")
.From<TableA>()
.From<TableB>()
.Where(TableA.YearColumn).IsEqualTo("TableB.Year")
.And(TableA.MonthColumn).IsEqualTo("TableB.Month")
.And(TableA.UseridColumn).IsEqualTo("TableB.UserId")
(I tried different ways in setting the IsEqualTo parameter)
IsEqualTo(TableB.YearColumn)
IsEqualTo(TableB.YearColumn.QualifiedName)
Either the parameter is interpreted as
TableA.Year = 'TableB.Year'
or I get a SqlQueryException.
Can somebody tell me how to do this query with subsonic (Either the first - with JOIN or the second one)? Thanks
With SubSonic 2 out of the box you can't.
This said, you have the following alternatives:
Extend SubSonic
If you're already familiar with SubSonic, you may consider to add multi-column joins to SubSonic itself.
Use views, Stored procedures, table functions
If you do not want to mess with SubSonics code, use views, stored procedures and/or table functions within sql server. SubSonic makes it easy to access data from views and stored procedures.
Use an InlineQuery
InlineQuery allows you to execute any sql - if it is an option to have bare sql in your code.
Ugly workaround with InlineQuery
If you absolutely want to create your query with SubSonic, you can try this:
SqlQuery q = DB.Select()
.From<TableA>()
.CrossJoin<TableB>()
.Where(TableA.YearColumn).IsEqualTo(0)
.And(TableA.MonthColumn).IsEqualTo(0)
.And(TableA.UseridColumn).IsEqualTo(0);
Build the SQL statement, and replace the parameter names:
string s = q.BuildSqlStatement();
s = s.Replace(q.Constraints[0].ParameterName, TableB.YearColumn.QualifiedName);
s = s.Replace(q.Constraints[1].ParameterName, TableB.MonthColumn.QualifiedName);
s = s.Replace(q.Constraints[2].ParameterName, TableB.UserIdColumn.QualifiedName);
Then use s with an InlineQuery.

LINQ To SQL "Group By"

I wonder if someone can help me. I want to replicate the following SQL query using LINQ in VB.Net.I'm a little unclear on how to do subqueries / aggregates.
Thanks
SELECT *
FROM Server S
INNER JOIN ServerHDD H
ON S.Server_ID = H.Server_ID
INNER JOIN (SELECT MAX(ServerHDD_ID) AS ServerHDD_ID
FROM ServerHDD
GROUP BY Server_ID, Letter) Filter
ON H.ServerHDD_ID = Filter.ServerHDD_ID
ORDER BY S.Hostname, H.Letter
Got this as below in C# => need VB.Net Conversion please.
from S in SERVER
join H in SERVERHDD on S.Server_ID equals H.Server_ID
join FILTER in
(from s in SERVERHDD group s
by new {s.Server_ID, s.Letter}
into groupedServerHDD select new
{
SERVERHDD_ID = groupedServer.Sum(gS=>gS.ServerHDD_ID)
}
)
on H.ServerHDD_ID equals FILTER.SERVERHDD_ID
orderby S.Hostname, H.Letter
select S
This is my most favorite page regarding this topic. I love LINQ to SQL (and wish they intended to continue support for it over Entity Framework...) http://msdn.microsoft.com/en-us/vbasic/bb688085.aspx. On this page you will find all the answers to your querying needs. It is hard to format the query here without something to test it against!
Your inner joins go away with the simple join syntax of LtS. You can either say .Max() on your inner select or Max(pseudo functoid here) like this:
From p2 In g _
Where p2.UnitPrice = g.Max(Function(p3) p3.UnitPrice) _
Select p2
There are a couple of LINQ learning tools out there that are pretty cool.
This one is my favourite... LinqPad. But you might also want to check out Linqer.
You should be able to paste your code into one of them apps and it will show you how its converted. Hope they come in handy :)