Email address as a Table in SQL - sql

I'm just wondering if it is OK to used the email address as a Table in SQL script? for example,
abcd_activity#cdf.com up
select distinct usr.user_name as USER_NAME, usr.id as USER_ID
from abcd#cdf.com usr, abcd_activity#cdf.com -- <-----------------------
where usr.id = ua.id
group by usr.name, usr.id;
My problem is that, I received the log file containing the error of the script and I noticed that the table used is email address. So, my assumption is that the caused of the error is in the table itself. Or, is it OK to use an email address as a lookup table in script?

This is a bit long for a comment.
No, it is not all right to have a table name like abcd_activity#cdf.com, for the simple reason that . and # are not allowed as table names. So, any attempt to do:
from abcd_activity#cdf.com
is going to result in a syntax error.
SQL does allow you to escape table names. You can get around this problem with one of these:
from "abcd_activity#cdf.com"
from `abcd_activity#cdf.com`
from [abcd_activity#cdf.com]
(which depends on your database).
More importantly, though, is that there is no sensible reason (that I can think of) to have a table represent separate emails. Normally, email would be a column in a table, not a table name.
So, you would have tables like:
Users Table
userId userName email . . .
And UsersActivity table:
userActivityId userId . . . .
email would be a column in Users, which you would look up using a JOIN.

Related

Select statement in SQL Server returns null while fetching values using email in where; unable to fetch data using values with dots using select

I'm trying to get password from email address but the problem is that the selection query won't read the the values after dots in the emails column and returns null results. I've tried PARSENAME() and RIGHT() functions to get it read the full email address. But no luck there. I've put my query statement below.
select Password
from Faculty
where Email = 'john.carson#outlook.com'
I'm not getting the password that belongs to that email address, instead all I get is null. The datatype is both email and password fields are nvarchar(100). And there are 5 records with different emails one of which is this. I'm sure the problem's rising because of the dots, because I tried using dummy data in email as like johncarson#com and I got the password respective to that. I'm using SQL Server as my database engine.
I Hope, It will be work
select Password
from Faculty
where Email like 'john.carson#outlook.com'
I checked with dummy data but it's working please check with Like clause may be any space is exists in Email field.
Declare #Faculty Table (Email Varchar(300),Password varchar(20))
Insert into #Faculty VALUES ('john.carson#outlook.com','123'),('john1.carson#outlook.com','12335'),('john2.carson#outlook.com','12356')
select Password
from #Faculty
where Email = 'john.carson#outlook.com'

PostgreSQL match a string from a nested query

I am trying to run a query where we find movies based on an actors name. When I run
SELECT known_titles
from name_basics
where name like 'Tom Cruise'
The datasource we have returns a single TEXT field of tt0181689,tt0092099,tt0116695,tt0325710.
How could I use this to query again? My main query is:
SELECT movie_name
from MOVIE_INFO
where movie_id like (SELECT known_titles
from name_basics
where name like 'Tom Cruise'
)
But this is returning nothing. Is there a way I can do a movie_id wildcard search in PostgreSQL that will see what is similar? Something like SELECT movie_name from MOVIE_INFO where movie_id in tt0181689,tt0092099,tt0116695,tt0325710?
The list of tt0181689,tt0092099,tt0116695,tt0325710 is stored as TEXT in a single cell and is copied from a CSV file using PostgreSQL \copy command.
The query with the subselect will cause an error if the subselect returns more than one row. Your code probably doesnt handle errors appropriately, but ignores them, so that the result appears empty.
You will have to learn the basic SQL concept of “join”:
SELECT movie_info.movie_name
FROM movie_info
JOIN name_basics
ON movie_name.movie_id = name_badics.known_titles
WHERE name_basics.name = 'Tom Cruise'
Remarks:
The table and column names are not very suggestive.
Using LIKE doesn't make sense if there is no wildcard in the pattern.
The last sentence of your question does not make sense to me at all. The list is the result of a query: how can it come from a file at the same time?

VBA Access Table reference in SQL query

I have been running into trouble executing SQL code in VBA Access when I refer to certain Table names.
For example,
INSERT INTO TempTable (ClientName) SELECT DISTINCT 1_1_xlsx.ClientName FROM 1_1_xlsx'<--does not work
The code works fine when I changed the Table name from 1_1_xlsx to Stuff.
INSERT INTO TempTable (ClientName) SELECT DISTINCT Stuff.ClientName FROM Stuff '<--works
I have no idea why the first query results in a syntax error and the second code is runs fine even when they refer to the same thing. I suspect it should be the naming conventions but I could not find any concrete answers.
Also, are there any ways that I could use 1_1_xlsx as my table name? Or am I just writing my query wrong?
try this:
INSERT INTO TempTable (ClientName) SELECT DISTINCT [1_1_xlsx].ClientName FROM [1_1_xlsx]
In many SQL based databases you can't have a table name or field name that starts with a number.
I suspect this is the underlying reason for your problem. Although Access will allow it, I have seen it cause problems in the past.
The problem is the number at the beginning of the table name. That is bad -- because it confuses the parser.
This is a bad table name, but SQL allows you to define table aliases. And, in this case, you don't even need to repeat the table name. So, here are two simple solutions:
INSERT INTO TempTable (ClientName)
SELECT DISTINCT ClientName
FROM 1_1_xlsx;
Or:
INSERT INTO TempTable (ClientName)
SELECT DISTINCT t.ClientName
FROM 1_1_xlsx as t
There is no reason to use the complete table name as an alias. That just makes the query harder to write and to read.

Need to select from SQL where in a list

I have an SQL table and I need to select all the records where username (MM_Session) is in another users following list (Users SQL table, following column), which would look like this
username1 username2 username3 username4
So far, my SQL query looks like this:
SELECT *
FROM Users
WHERE Users.Following_By LIKE '" . $_SESSION['MM_Username'] . "'
Except that doesn't seem to return any values even though the user's username is in the other users following list.
Should I be using 'IN' instead of 'LIKE'?
Does anyone have any other ideas why this isn't working?
Thanks
The LIKE operator works just the same as = if you don't use wildcard characters in your query string. Place them before and behind the word to match any occurrence of the word:
SELECT *
FROM Users
WHERE Users.Following_By LIKE '%" . $_SESSION['MM_Username'] . "%'
Though you might get more users than you expected because it matches for example foobar if MM_USERname contains only foo. You could use explicit delimiters to prevent this. Or, even better, use a separate table for the list entries and use a foreign key to join it to the Users table.

Simple sql query problem

Hi I have this query in ms access which is somehow not working. All I need to do is to pull out donator name from donator table with the id of volunteer. But I need to get user input, volunteer name and pull out the related volunteer first. Please help.
SELECT volunteer.id, volunteer.name, donator.* FROM volunteer, donator WHERE Volunteer.id = Donator.vid AND Volunteer.name = Forms!frm5!Combo2;
Check your case on Database names. For instance, you reference volunteer.name and Volunteer.id. In some DB's, fields are case sensitive.
Also, you'd want to quote your volunteer name field.