SELECT _cotM.Customer_ID, _cotM.Material_ID
FROM dbo.COT_Monthly AS cot
INNER JOIN
dbo.vw_Dim_Material AS matr ON cot.Material_ID = matr.Material_ID
GROUP BY _cotM.Customer_ID, _cotM.Material_ID
I have sql code and have result 25855 rows
but when i add where matr.Brand <> '%VIT%' upper group by the result still 25855 rows.
but when i delete wildcard where matr.Brand <> 'VIT' the result became 25089.
i wandering why the result like this ?
is i am wrong using wildcard or else?
Thanks for answering.
Wildcards only work with LIKE clauses, so change the condition:
WHERE matr.Brand NOT LIKE '%VIT%'
as Matt says use where matr.Brand not like '%VIT%',read more about wildcard
Related
I want to join two tables on a like clause. My schema is as the link:
Sql Fiddle 1
As you can see the result is empty.
But if I use like as follows:
Sql Fiddle 2
As you see I can get the results with name or des contains 'GRE'. So what's the problem here?
I searched the answer for a while, and found the suggested way to do this is the same as I did:
similar question
Any suggestions will be highly appreciated.
You are doing it correctly. you just have to use TRIM() to remove trailing whitespaces. Use it like
SELECT
a.id as app_id,a.app_name,a.des,
b.id as tag_id, b.name as tag_name
FROM aa_t_aaaa_app a
JOIN aa_t_aaaa_tag b
ON ((a.app_name LIKE CONCAT('%', TRIM(b.name) ,'%')) or (a.des Like CONCAT('%', TRIM(b.name) ,'%')))
order by a.id`
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
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')
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
I have this query:
select feature_requests.*,
from feature_requests
where feature_requests.status in ('open','closed','indevelopment')
I also have another status - denied.
I need to also select all rows with status denied but another column on my features request table must equal something.
So something that does this:
select feature_requests.*,
from feature_requests
where feature_requests.status in ('open','closed','indevelopment','denied') and
if status = denied, instance_id = ?
Not sure of the correct syntax. Thanks for any help :)
The WHERE clause is the correct place to put these kind of conditions, but with a few differences:
SELECT `fr`.*
FROM `feature_requests` fr
WHERE (`fr`.`status` IN ('open','closed','indevelopment')) OR
((`fr`.`status` = 'denied') AND (`fr`.`instance_id` = ?))
P.S - Notice I'm using an alias for feature_requests called fr instead of writing the whole name again and again. And You don't have to write its name at all because you're using only one table in your query, but I would still use it because it reduces the chances of future mistakes.
For further reading - SELECT Syntax
From rusty memory, you're probably wanting something like this
SELECT feature_requests.* FROM feature_requests
WHERE feature_requests.status IN ('open', 'closed', 'indevelopment') OR
(feature_requests.status='denied' AND instance_id = ???)
What you have right now is pretty close.
http://www.tutorialspoint.com/sql/sql-and-or-clauses.htm
This should work:
SELECT
feature_requests.*
FROM
feature_requests
WHERE
feature_requests.status IN ('open','closed','indevelopment') OR (
feature_requests.status='denied' AND
instance_id=?
)
This can also be written without listing the table name over and over if it is the only table that you are using like this:
SELECT
*
FROM
feature_requests
WHERE
status IN ('open','closed','indevelopment') OR (
status='denied' AND
instance_id=?
)
When using AND and/or OR in your where clause please also remember to use parenthesis ( ) to show your actual meaning even when you know what takes precedence between the AND and OR. For more information on precedence of operators with MySQl Example:
color=blue AND shape=circle OR type=ball
means
(color=blue AND shape=cirlce) OR type=ball
but could easily be misinterpreted as
color=blue AND (shape=circle OR type=ball)