SQL Email (.com) Reference - mysql

My school work has asked me to retrieve information from all customers with emails ending in ".com" I cannot find any information anywhere. Can you help me out? Below is what I have tried as well as many other variations.
SELECT first_name AND last_name FROM customerform WHERE email LIKE('.com')

That's not how LIKE works, nor how SELECT works. Instead:
SELECT first_name, last_name FROM customerform WHERE email LIKE '%.com'
Here % means "begins with" or "blah blah blah" in more casual parlance.
Remember things like SELECT are fully documented and there's no shame in checking how they work before running your statement. We all need to check that we're doing it correctly.

Related

SQL find values that starts with a string

So, i have an online website.. and administrators have a name [ADM]Name. I want to block users to register with name of [ADM].
SELECT name FROM users WHERE name LIKE ']%'
Is this query good to select users who wish to make name with adm name ?
Note: I don't want to specify ADM, i want just [ ] tags
SELECT name FROM users WHERE name LIKE '[ADM]%'
In most databases, you can do:
where name like '[%]%'
or if you specifically want "ADM":
where name like '[ADM]%'
Some databases extend like patterns, but MySQL is not one of them.

Prompt user to input a variable in MySQL

At school, I believe I work with Oracle SQL Developer when writing SQL. And in this I can type:
SELECT Book_Title, Auth_ID
FROM book
WHERE Auth_ID = '&Enter ID';
This will then display a little message box where the user can enter an ID number to see all the books written by an author with that ID number.
I want to know if there is a way to do this in MySQL. I have looked and the nearest thing I can find is setting a variable before hand, which is not quite what I'm looking for:
SET #EnterID := 2;
select Book_Title, Auth_ID
from book
where Auth_ID = #EnterID;
The above statement in MySQL will return all the books with author ID of 2, but only because I set it to that previously. I want the user to be able to enter the variable.
Thanks.
Oracle has the concept of interactive queries, those that as you said you can run by adding the '&' before your variables names, that is a variable substitution, this concept doesn't exist in MySql, MySql is not interactive and requires the user to enter the values in the variables by using the keyword 'SET' and # (instead of & like in Oracle).
So, no, you cannot do what you are looking for since this is not a client-side implementation either.
BTW, I just noticed this was asked so many years ago, amazing that this is still not added as a feature in mysql.
For a prompt, you must put the char ':' followed by the name of the variable
Example :
select *
from YOUR_TABLE
where YOUR_COLUMN = :your_var
mysql is to run SQL queries .SQL is a query language, it is not for user interaction
See : How to ask MySQL to prompt for values in a query?

ms access like ip addresses starts with sth

In microsft access I got a table with fields username and ip
I want to select all rows that their ip addresses starts with 37.255.
I tried this but it didnt work:
select sheet1.username , sheet1.ip from sheet1 where sheet1.ip like '37.255.*'
Thanx in advance
By "I tried this but it didnt work:" you mean, you get no records back?
The SQL syntax looks good to me. Did you define [ip] as a text field? The LIKE clause would not work, if [ip] was a number.

Delete where email is not a known email client

I'm trying to delete records from a table that is getting to much spam. So, the idea is delete all records that don't have email from hotmail, gmail or yahoo. But my sql looks like is returning some hotmail records:
SELECT *
FROM `users`
WHERE email NOT LIKE '*hotmail.com*'
AND email NOT LIKE '*gmail.com*'
AND email NOT LIKE '*ymail.com*'
Applying the OR condition returns the same result. Can you guide to the right direction?
The query should look like this
SELECT *
FROM `users`
WHERE email NOT LIKE '%hotmail.com%'
AND email NOT LIKE '%gmail.com%'
AND email NOT LIKE '%ymail.com%'
You have to replace * by %
If SQL LIKE clause is used along with % characters then it will work
like a meta character (*) in Unix while listing out all the files or
directories at command prompt.
Here's the doc about LIKE in MySQL
SELECT *
FROM users
WHERE email NOT RLIKE '\\#(hotmail|gmail|ymail)\\.com$'
Your query will also return you gmail and ymail
In mysql you have to use % instead of *
So just substitute it and everything will be ok.

Escaping % sign in subquery

I have a query in mySQL that's meant to return search terms that are used on our site. Yes, this is for a tag cloud, and yes, I know it's a mullet :)
We've got an admin page where administrators can view search terms and choose to exclude them from showing up in the cloud. These words go into the "badWords" table. We've gotten some terms like "foo%2525252525252520bar", and we're having trouble getting those excluded.
In pseudocode, the query to get the search terms for the cloud is:
SELECT * FROM `searchTerms` WHERE `word` NOT IN ( SELECT `word` FROM `badWords` )
This works fine, unless one of the terms returned from the subquery has a % in it. Is there a way to escape the entire subquery? I've tried doing a
replace( SELECT `word` FROM `badWords`, '%', '\%' )
... but that's apparently not syntactically correct.
I can do two queries if need be, but wondered if there's a way to get it done as is.
Thanks!
==============================
UPDATE: closing this for now, as I think the error lies elsewhere. Will report back once I know for sure, but don't want folks wasting time answering the question here if it's not the correct question...
Upvoted both of the replies received so far. Thanks, guys.
==============================
UPDATE 2: sigh Nevermind... can't close it :\
==============================
FINAL UPDATE: Well, looks like escaping the value isn't the problem. The admin page passes the value in the URL before it's added to the badWords table. In passing the value via the URL, it changes. So what's added to badWords is actually "foo%25252525252520bar" (there's one less "25" sequence). If I manually update the value in badWords and add back the missing "25" it works as expected. So no need to replace or escape anything. I just need to fix those URL values properly.
==============================
I don't think the % is your problem here. I think that you're trying to use REPLACE() on the subquery itself (SELECT ...), and not on a column value (word). Try this instead:
SELECT * FROM `searchTerms`
WHERE `word` NOT IN (
SELECT REPLACE(`word`, '%', '\%') AS word FROM `badWords`
);
Good luck!
I'm not very good with MySQL syntax, but SQL Server let's you do it this way:
SELECT * FROM `searchTerms` WHERE `word` NOT IN ( SELECT REPLACE(`word`, '%', '\%') FROM `badWords` )
NOTE: Basically all I did was move your REPLACE over some =) Hope this helps.