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.
Related
I have a Table with Field "UserId" in Postgres
The List Contains a list of UserIds (comma separated) but also might contain email addresses and phone numbers .
Example
ID
UserID
1
11111,22222,+9199999999,xyz#yxz.com
3
2222,3333,11,+887777777,abc#bca.com
I want to remove all the phone-numbers and Email addresses and get the list of all userids comma separated in a new field.
OUPUT
ID
UserID
1
11111,22222
3
2222,3333,11
Also it will be better to have the query being optimised as it will be a part of a much complex query and should not not impact the performance .
Can someone suggest an ideal way to do it ?
Thanks
I have tried SUBSTRING , SPLITPART and Case Conditions based on it , but couldn't come out with a proper solution.
Use the REGEXP_REPLACE function to remove phone numbers.
UPDATE tablename SET columnname = REGEXP_REPLACE(columnname, '(\+[0-9]{1,3}[- ]?)?[0-9]{10,}', '');
Use the REGEXP_REPLACE function to remove email addresses.
UPDATE tablename SET columnname = REGEXP_REPLACE(columnname, '[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,}', '');
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.
I am using oracle database and trying to find out the query which should return the result when there is a special character(',`,(,)) exist on the string.
I am trying something like this,
select username from users where username like (',`,~,(,));
I tried to achieve the same using the below query,
select username from users where (username like '%`%' OR username like '%~%');
It doesn't consider the second condition and returns the value to the first condition only.
Is there any function/methods using which this result can be fetched?
You can use regular expressions and check all special characters with one condition:
SELECT username
FROM users
WHERE regexp_instr(username,'[''`\(\)]') > 0
Old school style without regexp
where length(translate(username, '_''`~()', '_')) <> length(username)
In my SQL table, I have a column named "user_id" with comma separated value like this: a,b,c,d and I just wonder how can I update this column without removing old values. I want to update this column to a,b,c,d,e and in other step to a,b,c,d,e,f.
I wrote this query, but it removes old values and does not not update values with comma separated list:
UPDATE multiusers SET user_id = '" . $userID . "' WHERE hwid = '" . $hwid."'
If you want to update a column and append a value just do :-
UPDATE multiusers
SET user_id = user_id + "new user"
WHERE hwid = $hwid
This just appends a value to the existing value in SQL SERVER db at least, other databases may have different concatenation methods. I think your questions points to a fundamental design issue in your database and I would suggest rethinking this.
You are treating a field as a table. Even when you can do, it's a very bad approach. You should have as many records on multiusers table as userid's you have. But if you insist in your approach then you will have to create a quite complex query to retrieve the value of userid field, move to an array and compare it with the new value to make sure you doesn't insert duplicates in the field. Something like a cursor may work for you.
I want to make a query in order for the user to inputs the name of a table and then it shows him the results of that table. Is that possible? I am using Access 2013.
You may try the syntax below :
SELECT Table.column FROM Table
WHERE (((Table.column) Like "*" & [your message to prompt input:] & "*"));