case sensitive select query in mysql - mysql

Here Problem is make sql query is not check case sensitive latters
Here SQL Version;
VERSION(): 8.0.15
Sql Query for login user:
SELECT COUNT(*) as cnt FROM accounts WHERE (number = 'AdMin' OR email = 'AdMin') AND password = '******' AND type IN (1) AND deleted = 0
Database store name='admin'
here how to set case sensitive sql query for mysql 8.0 and stop login when username ='AdMin'

You need to use the BINARY operator to make your search case sensitive:
SELECT COUNT(*) as cnt
FROM accounts
WHERE (number = BINARY 'AdMin' OR email = BINARY 'AdMin') AND password = '******' AND type IN (1) AND deleted = 0
Demo on dbfiddle

Related

IF-ELSE in Postgres

I'm failing to translate a SQL query which was designed for MySQL into Postgres syntax. This is the query:
select if(sendonly = true, 'REJECT', 'OK') AS access from accounts where username = '%u' and domain = '%d' and enabled = true LIMIT 1;
This nice little function "if()" is not available in Postgres. My first attempts with some CASE clauses failed. What needs to be modified to make this query run in Postgres?
As you noted, you could use a case expression:
SELECT CASE WHEN sendonly = true THEN 'REJECT' ELSE 'OK' END AS access
FROM accounts
WHERE username = '%u' AND domain = '%d' AND enabled = true
LIMIT 1;
Or, as Postgres can evaluate booleans directly, you could trim this query down a bit:
SELECT CASE WHEN sendonly THEN 'REJECT' ELSE 'OK' END AS access
FROM accounts
WHERE username = '%u' AND domain = '%d' AND enabled
LIMIT 1;

MySQL CASE/IF usage

My application currently connects to MySQL and performs this query:
select nickname
from users
where nickname = '$nick' and password = '$pass'
$nick and $pass are inserted by the application.
I need to advance this query to do the following:
If the where nickname = '$nick' and password = '$pass' returns a row just return the row as usual.
If the where nickname = '$nick' and password = '$pass' return 0 rows, I need it to check whether the '$nick' is present, and it is the fact that '$pass' is wrong that there are 0 rows, if this is the case then return 0 rows, other wise if there is no '$nick' in the table, return the row anyway.
i.e. if nickname is in the table and the password is correct, or the nickname isn't in the table, return a row. Otherwise if nickname is in the table and the password is not correct, return 0 row.
Is this possible using MySQL?
Try this:
select nickname
from users
where nickname = '$nick' and password = '$pass'
union
select '$nick' as nickname
from users AS t1
where not (nickname = '$nick' and password = '$pass') and
not exists (select 1
from users
where nickname = '$nick')
Demo here

How to combine these two credentials Queries?

I wanted to make this two queries into one query. The purpose of the query is to check if the credentials match in either of the table "user_credentials" or "workers". How can I do so?
SELECT user_name FROM user_credentials WHERE user_name = '$user_email'
AND password = '$user_password'
SELECT user_name FROM workers WHERE user_name = '$user_email'
AND password = '$user_password'
try this
SELECT user_name FROM user_credentials WHERE user_name = '$user_email' AND password = '$user_password'
union
SELECT user_name FROM workers WHERE user_name = '$user_email' AND password = '$user_password'
then check mysqli_num_rows or mysql_num_rows

SQL fails in PHP but works in command line mysql

SQL Code:
SELECT id, album_date AS timestamp, CONCAT((SELECT detail_value
FROM people_db.user_details_tbl WHERE detail_field = 'first_name' AND user_id = pictures_db.albums.owner), ' uploaded pictures!') AS title_html
FROM pictures_db.albums
WHERE id IN
(SELECT DISTINCT(album_id)
FROM pictures_db.album_pics
WHERE pic_id IN
(SELECT DISTINCT(picture_id)
FROM pictures_db.picture_access_tbl
WHERE grantee_group_id IN
(SELECT group_id
FROM people_db.group_membership_tbl
WHERE member_id = '2'
)
)
);
PHP Code:
$albums_sql = mysql_query("SELECT id, album_date AS timestamp, CONCAT((SELECT detail_value
FROM people_db.user_details_tbl
WHERE detail_field = 'first_name' AND user_id = pictures_db.albums.owner), ' uploaded pictures!') AS title_html
FROM pictures_db.albums
WHERE id IN (
SELECT DISTINCT(album_id)
FROM pictures_db.album_pics
WHERE pic_id IN (
SELECT DISTINCT(picture_id)
FROM pictures_db.picture_access_tbl
WHERE grantee_group_id IN (
SELECT group_id
FROM people_db.group_membership_tbl
WHERE member_id = '2'
)
)
)") or die(mysql_error());
When the PHP is run, the error is: Table 'pictures_db.albums' doesn't exist
I tried executing as the same user, regranted all permissions, and even flushed privileges. Works in shell, not in PHP.
Any ideas?
The error message is quite clear: MySQL sees the database pictures_db but not the table albums.
That could be due to permissions, but you seem to have checked that thoroughly.
Another possible reason is that the connection string you're using in PHP points to a different database instance than the one you're using at the command line. Perhaps the connection string still points to a different environment, such as DEV but you're in QA or points to an old test version of the database?
Do you call mysql_select_db() before running the query?
mysql_select_db('pictures_db');

How to use Boolean logic in a mySQL SELECT query

How can I check if email = '$e' or username = '$e' inside my MySQL query.
Here is my MySQL query so far.
"SELECT user_id FROM users WHERE (email = '$e' AND pass=SHA1('$p'))"
If you want to modify you existing query so that it works even if $e matches username, you can do:
SELECT user_id
FROM users
WHERE (email = '$e' OR username = '$e') AND pass=SHA1('$p')