Query to retrieve values of similar primary keys - mysql

I have a table with two columns which looks like this:
+-----------+--------+
| Mobile_id | Person |
+-----------+--------+
| ME_02_05 | John |
| ME-02 05 | Barry1 |
| 02-05 | John |
| ME 03-02 | James |
+-----------+--------+
I want to retrieve all persons whose Mobile_id contains integers (02 05). It can be seen that there are different combinations of 02 05 in the table such as ME_02_05, ME-02 05 and 02-05.
Can anyone please tell me how to do this?

SELECT Person FROM Persons WHERE Mobile_Id LIKE "%02%05"
should work.

SELECT Person FROM t1 WHERE Mobile_id REGEXP '02[-_ ]05'
This selects each Person entry that has 02 followed by dash, underscore, or space followed by 05.

Related

Find most recent records with specific criteria in Access

I have a set of records that I would like to display the most recent records that match certain criteria. I've done it wrong in the past where it would first pull the most recent records and THEN go and try and match criteria which would cause some of the records to disappear. What I want to have the query do is to find the records that match criteria first and THEN have it pull the most recent records from that data set. I need to have this query INSERT INTO a Table in Access.
I thought I had it sorted out, but I get an error "Your query does not include the specified expression 'SufGrpID' as part of an aggregate function
An example of the data:
When the query runs, I would like the results to be:
An example of the data:
SufGrpID 03 would be removed from the set because it is not the newest record for CaseID 123
SufGrpID 04 would be removed from the set because it is not of SufTypeID 14 and it is not of Status F
How the data looks
+----------+---------+-------------------------+-----------+--------+
| SufGrpID | CaseID | CreateDate | SufTypeID | Status |
+----------+---------+-------------------------+-----------+--------+
| 01 | 123 | 2010-08-20 07:42:32.000 | 14 | F |
| 02 | 234 | 2010-04-28 10:33:56.000 | 14 | F |
| 03 | 123 | 2010-04-20 10:05:04.000 | 14 | F |
| 04 | 345 | 2010-08-20 11:18:42.000 | 12 | I |
| 05 | 345 | 2010-04-20 11:18:42.000 | 14 | F |
+----------+---------+-------------------------+-----------+--------+
Here's the code that did not work for me...
INSERT INTO [aStudent Base Data] ( [Self Suff ID], [Self Suff Create Date] )
SELECT dbo_sufscrgrp.SufGrpID, Max(dbo_sufscrgrp.CreateDate)
FROM dbo_sufscrgrp
WHERE (((dbo_sufscrgrp.SufTypeID)=14) AND ((dbo_sufscrgrp.Status)="F"))
GROUP BY dbo_sufscrgrp.CaseID;
What I'd like the results to be. (EDITED at 1:33 CST)
+--------------+------------------------+
| Self Suff ID | Self Suff Create Date |
+--------------+------------------------+
| 01 | 2010-08-20 07:42:32.000 |
| 02 | 2010-04-28 10:33:56.000 |
| 05 | 2010-04-20 11:18:42.000 |
+--------------+-------------------------+
Thanks for any help you can give!
Based on the minimal dataset example, consider:
SELECT dbo_sufscrgrp.*
FROM dbo_sufscrgrp
WHERE SufGrpID
IN (SELECT TOP 1 SufGrpID FROM dbo_sufscrgrp As Dupe
WHERE Dupe.CaseID=dbo_sufscrgrp.CaseID AND SufTypeID=14 and Status="F"
ORDER BY Dupe.CreateDate DESC, Dupe.SufGrpID DESC);

Concatenate values for concatenated IDs

Database: MySQL
I have two tables, one for user's assigned roles and one that contains the role information. My problem is that the assigned roles are stored in a single field, separated by commas. I need to build a report that lists the roles by name, not the id, but still be in a single field separated by columns.
I'm thinking GROUP_CONCAT might be the solution but I've seen it used to create a concatenated list, not use one that already exists.
Table 1:USERS
ID | FNAME | LNAME | ROLE_IDS
------------------------------------------
1 | Bob | Jones | 445,44,45,449,459
2 | Mark | Doe | 426,459,445
3 | Jeff | Apple | 444,45
Table 2: ROLES
ID | ROLE_NAME
------------------------------------
4 | Basic
13 | Reporting
16 | Advanced
44 | Admin
45 | Super User
426 | Accounting
444 | User
445 | Receivables
449 | Processing
459 | Research
Expected Query Results:
ID | FNAME | LNAME | ROLES
-------------------------------------------
1 | Bob | Jones | Receivables, Admin, Super User, Processing, Research
2 | Mark | Doe | Accounting, Research, Receivables
3 | Jeff | Apple | User, Super User
For getting referencing role names, you can use GROUP_CONCAT like this :
SELECT us.ID,us.FNAME,us.LNAME,
GROUP_CONCAT(ro.ROLE_NAME) ROLES_NAME
FROM USERS us
INNER JOIN ROLES ro
ON FIND_IN_SET(ro.ID, us.ROLE_IDS) > 0
GROUP BY us.ID
I've tested it in SQLFIDDLE and working fine.

How to prevent insert in distinct 2 columns combination in mysql [duplicate]

This question already has an answer here:
MySQL unique index by multiple fields
(1 answer)
Closed 6 years ago.
I have a mysql table that looks like this:
id | FirstName | lastName | age
--------------------------------
But when I inserted some data my table that looks like this.
id | FirstName | lastName | age
--------------------------------
01 | praneeth | madusanka| 22
02 | praneeth | praneeth | 23
03 | madusanka | praneeth | 25
04 | praneeth | madusanka| 33
05 | damith | asanka | 43
06 | damith | danushka | 22
07 | damith | asanka | 33
08 |asanka | damith | 44
But I wont to prevent id=04 data. Because id=01 and id=04 is same data. How I create insert query for preventing above data, for inserting my table.
Try to create composite key (FirstName + Lastname)
If a primary key consists of two or more columns it is called a
composite primary key.
Add a unique key with columns firstname and lastname. This would prevent duplicate combinations.
Try this:
ALTER TABLE table_name ADD UNIQUE unique_index(FirstName, lastName);

Fastest way to insert data to database for one to many relationship

I am looking for a fastest way to insert data into database.
Currently I have 2 tables which is "User" and "User_Detail".
One "User" can has many "User_detail"
Example:
In database,we have the record of Age and mail for user "John".
User table
|Name |
|---------|
| John |
| Jason |
| Wilson |
User_Detail table
| Usr_Name| Property | Value |
|---------+----------+--------|
| John | Age | 12 |
| John | mail | gmail |
| Wilson | Age | 31 |
I would like to write a query to add "uni" to ALL of the users.
The result will become like this.
User_Detail table
| Usr_Name | Property | Value |
|----------+----------+--------|
| John | Age | 12 |
| John | mail | gmail |
| John | Uni | 00000 |
| Wilson | Age | 31 |
| Wilson | Uni | 00000 |
| Jason | Uni | 00000 |
Is there any suggestions or ideas on how to insert data ?
I need the fastest way to do it, as I have around 10k users in my USER table.
It can be any language or database query, as long as it can be very fast to insert the record to database.
First, consider normalizing your schema. Here is an in-depth discussion of EAV storage on dba.SE.
With your given design, this does the job:
INSERT INTO "User_Detail" ("Usr_Name", "Property", "Value")
SELECT "Name", 'Uni', '0000'
FROM "User";
In Postgres, I would also advise not to use mixed-case identifiers.
To insert a value in, just do a simple insert query.
INSERT INTO `User_detail` (`User_name`, 'Property`, `Value')
SELECT `Name`, 'H/P', 50012 FROM `Users`
To make the inserted value be something different, you need to change that hard coded value 50012 to something that resolves to the number you want there.

How to update one table in MySQL after insertion in another table

I have two table first is "employee_details" second is "attendance_rule"
employee_details
e_code | e_name | e_type
01 | Ram | Regular
02 | Shyam | Contract
03 | Mohan | Regular
attendance_rule
e_code | e_type | casual_leaves
01 | Regular | 7
02 | Contract | 6
03 | Regular | 7
I have manually insert values in "attendance_rule".
Now the problem is that when the attendance rule changes in the organisation. The user has to manually update the casual_leave coloumn for all the employees.
What I want is that when an employee details are added in employee_details table , the entry in the attendance_rule table should be automatically made having e_code and e_type same as e_code and e_type of employee_details table and casual_leaves equals to the casual_leaves of the same type of employee.
I think that a trigger should be used here.
but I dont know how to use trigger for this condition i.e. for the entry of casual leaves.
Please help me...how should I do that? Is there a way to do it other that trigger?
It sounds like you don't need a separate entry in attendance_rule for every employee, just for every employee type, so you don't need a trigger at all.
Create employee_details like you suggested:
e_code | e_name | e_type
01 | Ram | Regular
02 | Shyam | Contract
03 | Mohan | Regular
But create attendance_rule with only the rules for each type:
e_type | casual_leaves
Regular | 7
Contract | 6
If you need to find the number of casual leaves for a particular employee, join the two tables:
select
e.e_code,
e.e_name,
e.e_type,
a.casual_leaves
from employee_details e
inner join attendance_rule a on e.e_type = a.e_type
to get
e_code | e_name | e_type | casual_leaves
01 | Ram | Regular | 7
02 | Shyam | Contract | 6
03 | Mohan | Regular | 7
And if you want to make it as easy to use as a table, you can create a view:
create view emp_all_details as
select
e.e_code,
e.e_name,
e.e_type,
a.casual_leaves
from employee_details e
inner join attendance_rule a on e.e_type = a.e_type
Now you can simply say: select * from emp_all_details to get:
e_code | e_name | e_type | casual_leaves
01 | Ram | Regular | 7
02 | Shyam | Contract | 6
03 | Mohan | Regular | 7