MYSQL. Extract data with column filtered - mysql

I got a table like this:
mysql> select * from users;
+--------+----------+------------+-----------+
| userid | username | password | privilege |
+--------+----------+------------+-----------+
| 1 | user1 | password | 1 |
| 2 | david | goodboy | 1 |
| 3 | admin | mastermold | 5 |
| 4 | user4 | password4 | 1 |
| 5 | user5 | password5 | 2 |
| 6 | user6 | password6 | 1 |
| 7 | user7 | password7 | 1 |
+--------+----------+------------+-----------+
7 rows in set
Now, how to extract password of username who is called "david" by the only select query without "password" stored in it and result is in one field. (Don't accept "select * from users")?

Try
select password from users where username='david';

without using mentioning "password"?
well you could grab the column name into a variable and use that like this sqlFiddle
SET #columnName = (SELECT COLUMN_NAME FROM
INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'users'
AND ORDINAL_POSITION = 3);
SET #sql = CONCAT("SELECT ",#columnName," FROM users WHERE username = 'david'");
prepare statement FROM #sql;
execute statement;

Well, you mean that your query result shouldn't have the string "password", if I have got it correctly. Even though I am posting the answer, You should really consider #MarcB comment and try learning SQL first.
Your query should go like this
select `password` from users where username='david'
and lower(`password`) not like '%password%';

Related

mysql query returns empty set when data is there

+----+---------+--------------------+----------+------+---------------------+---------+-------------+------------+----------+
| id | pname | pmail | pgender | page | pdetails | pnumber | ddepartment | date | time |
+----+---------+--------------------+----------+------+---------------------+---------+-------------+------------+----------+
| 1 | karuna | karuna#gmail.com | female | 21 | hfsdh gfhdf shdh | 2332 | Surgeon | 2018-05-15 | 16:00:00 |
+----+---------+--------------------+----------+------+---------------------+---------+-------------+------------+----------+
This is my database table appointment.
Whenever I execute first two queries, they run perfectly
select * from appointment;(execute )
select * from appointment where pname = 'karuna';(execute)
but when I try to execute these two queries, the result I get is empty set
select *from appointment where pmail='karuna#gmail.com';(empty set (0.00 sec))
select *from appointment where ddepartment='Surgeon';(empty set (0.00 sec))

group_concat does not show all the values mysql

ModelName.all(:having=>"count(receipt_no)>1",:select=>"school_id,group_concat(id SEPARATOR ',') as f_ids,receipt_no,count(distinct id) as id_count,count(receipt_no) as rec_count",:conditions=>"receipt_no is not null",:group=>"receipt_no")
Output is
+------------+-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------+
| receipt_no | school_id | id_count | f_ids | rec_count |
+------------+-----------+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------+
| 1261 | 1783 | 2 | 557660,557661 | 2 |
| 14/15- | 1783 | 1209 | 68352,77056,113664,56320,68353,77057,113665,56321,68354,56322,68355,81923,173571,113667,56323,68356,94980,56324,68357,56325,68358,80390,56326,68359,80391,110599,56327,80392,885... | 1209 |
| 15- | 1783 | 112 | 344067,344068,344069,344070,344075,326923,373261,373262,345882,360218,344091,361755,347685,341542,347689,360233,351530,358705,352829,324674,341576,324684,360018,368469,371541,3... | 112 |
Here group_concat does not show all the values but the count of items as same as the count receipt no. Suppose the items in the f_ids column is more than 200 character then its not showing all the values . In other case it will show correct value
I got the solution
SET SESSION group_concat_max_len = 1000000;
Run this code in MySQL console, then this code will change default group_concat character limit to 1000000 characters.
If you want to use in rails console,you can use in this following way
sql = "SET SESSION group_concat_max_len = 1000000"
ActiveRecord::Base.connection.execute(sql)
Please note:
This configuration will work only in that session

check if the value in any of these columns mysql

I got table called 'pet' have the following values
id | name | nickname | dateofborn |
1 | test | jhon | 2009 |
2 | test2| test | 2010 |
3 | mike | NULL | 2010 |
3 | jhon | testor | 2011 |
I want to select all of columns that contain 'test' value either in name column or nickname column so i have this query didn't actually work for me:
SELECT * FROM pet WHERE name='test' OR nickname= 'test'
note that I exactly want test value not testor.
The query is right, I think you have some problems with your Table.
But try like below:
USE [yourDB es: animals];
SELECT * FROM [yourDB].pet WHERE (name='test' OR nickname='test');
try this.
SELECT * FROM pet WHERE name like 'test%' OR nickname like 'test'

MySQL: GROUP BY with custom hierarchical functionality

I've got a permission/privileges - table looking like this:
+----+----------+----------+------+-------+
| id | name | usertype | read | write |
+----+----------+----------+------+-------+
| 1 | test | A | 0 | 0 |
| 2 | test | MU | 1 | 1 |
| 3 | test | U | 1 | 1 |
| 4 | apple | A | 1 | 1 |
| 5 | apple | MU | 1 | 0 |
| 6 | apple | U | 0 | 0 |
| 7 | flower | A | 0 | 0 |
| 8 | flower | MU | 0 | 0 |
| 9 | flower | U | 1 | 1 |
+----+----------+----------+------+-------+
there are 3 usertypes: A (admin), MU (maintenance user), U (standard user)
the usertypes are hierarchical: A > MU > U
(the usertypes are saved as CHAR(2) in the database, and unfortunately I can't change that)
now i want to build a query which implements the hierarchical logic of my usertypes.
e.g. usertype 'A' got no permission to read or write on stuff with the name 'test', thus usertypes 'MU' AND 'U' also should have no permission for that and their read = 1 and write = 1 should be ignored.
I know which usertype is currently logged in.
I somehow have to check for the minimum of read/write rights to the name for all hierarchical predecessors, i guess. but i don't know how to check that since usertype is not a number field.
this is what I've tried so far:
SELECT
name,
MIN(read),
MIN(write),
CASE
WHEN usertype = 'A' THEN 0
ELSE (CASE
WHEN usertype = 'WU' THEN 1
ELSE 2
END)
END userval
FROM
permissions
-- WHERE usertype <= :current_usertype
GROUP BY name
this seems to work, but i don't know how i can get my condition WHERE usertype <= :current_usertype working, so a usertype down in the hierarchy can't get more privileges on a name than a "higher" usertype.
any ideas?
thanks in advance!
This is how I solved my problem:
1. I added another table "permission_groups" to the database:
+----+----------+--------+
| id | usertype | value |
+----+----------+--------+
| 1 | A | 100 |
| 2 | MU | 20 |
| 3 | U | 10 |
+----+----------+--------+
2. Then I joined this table to my original table "permissions" which i showed in my question:
here i get the value of my "permission_groups" table with a subquery. this value symbolizes the hierarchical order of my different usertypes.
SELECT
perm.name,
MIN(perm.`read`),
MIN(perm.`write`),
group .value
FROM
permissions perm
LEFT JOIN permission_groups group ON group.usertype = perm.usertype
WHERE
group.value >= (SELECT value from permission_groups WHERE usertype = :current_usertype)
GROUP BY perm.name
:current_usertype is a PDO parameter in my case, which is replaced by the usertype of the current user.

MySQL best approach for db normalising, relationships and foreign keys

There is an username/password verification step first then the database has following structure
^ is primary key
* uses foreign key
1.StudentDetails table
===========================================================================
ID^| Username | Password | Email | Address * | Website |Comments
====+============+==========+=============+===========+=========+==========
1 | xxxxxxxxxx | xxxxxxx | xx#xxx.xxx | 1 | http:// | text
2.Submissions table
===========================================================================================
ID^|Username*|SubmitDate|SelectedCourse*|Price*|Promotion*|SubmitComments|SubmitStatus*
===+=========+==========+===============+======+==========+==============+=================
1 |xxxxxxxxx|2013-7-12 | int | int | int | text | int
3.SubmitComplete table
==================================================
ID^| Username * | SelectionDate | SubmitStatus *
====+============+===============+================
1 | xxxxxxxxxx | 2013-08-01 | int
Now I'm having an issue entering the address, when i try to enter the student details it won't accept until there is an address field, how best to tackle that?
When i do an left join selecting certain fields from StudentDetails and certain fields from Addresses, addresses don't show.
Im a mysql noob, so i'd like some guidance to see if the normalising and structure has been done correctly, or could it be done better, here is the fiddle i couldn't get it to work properly, kept getting errors on the lines where i added the foreign keys, even though the building of the schema worked well on my machine.
The fiddle console says error on line 2 but it looks to me it's actually on line 76.
If there's anything i was unclear on, pls let me know.
Thanks
Ok let me explain you how it would be. I made an example with two tables that you can see below.
Then you can create your query.
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| addresses |
| students |
+----------------+
2 rows in set (0.00 sec)
mysql> select * from students;
+----+----------+-----------+
| id | name | last_name |
+----+----------+-----------+
| 1 | jhon | smith |
| 2 | anderson | neo |
| 3 | trinity | jackson |
+----+----------+-----------+
3 rows in set (0.00 sec)
mysql> select * from addresses;
+----+-----------------+---------+
| id | address | student |
+----+-----------------+---------+
| 1 | Av 1 2nd Street | 1 |
| 2 | Av 3 4 Street | 2 |
| 3 | St 23 7 Av | 3 |
+----+-----------------+---------+
3 rows in set (0.00 sec)
mysql> select s.name,s.last_name,a.address from students s join addresses a on a.student=s.id;
+----------+-----------+-----------------+
| name | last_name | address |
+----------+-----------+-----------------+
| jhon | smith | Av 1 2nd Street |
| anderson | neo | Av 3 4 Street |
| trinity | jackson | St 23 7 Av |
+----------+-----------+-----------------+
3 rows in set (0.00 sec)
option 1:
enter the adress data first, and use that id when createing the row in StudentDetails
option 2:
change the field StudentDetails.Address so its allow NULL valus, enter the StudentDetails, then the adress, and then update the StudentDetails.Address
It's better to use foreign keys in number format So the Username should be the id of the StudentDetails table.
Can you put the SQL Query that you had tried to run?