I have a MySQL database with 2 columns. Each column is populated with numbers.
I need to return all records that matches the following criteria:
It will check the first character from column1. If it equals 1 then it will check the last character from column2. If it equals 7 then it will count the total length of column1 + column2. If it equals 12 then it will return this results.
Pseudo code (Vba)
For k = 1 To 100
col1 = Cells(k, 1).Value
For v = 1 To 100
col2 = Cells(v, 2).Value
If Left(col1, 1) = "1" Then
If Right(col2, 1) = "7" Then
If Len(col1) + Len(col2) = 12 Then
MsgBox col1 & "-" & col2
End If
End If
End If
Next
Next
Thanks.
The function names in mysql are almost identical: left(), right(), char_length(). You need to combine them in the where clause with and operator because you want to return records that satisfy all criteria.
select * from yourtable
where Left(col1, 1) = '1' and Right(col2, 1) = '7' and char_length(col1) + char_length(col2) = 12
Related
I have a table in Access 365 with this column:
"tblfreq"
Col1
0
0
1
0
0
0
0
0
1
0
0
1
0
I want to create a query that shows ranks/count(?!) rows between the value "1" of "Col1".
"Col1" only has "0" and "1".
That would create a query with a column "colaux" result like this:
Col1
Caux
0
1
0
2
1
1
0
2
0
3
0
4
0
5
0
6
1
1
0
2
0
3
1
1
0
2
as June 7th said there must be a unique identifier for each row. just add an autonumber column to the table. then you can calculate caux. if you have a fresh autonumber column which will go 1,2, 3, 4, 5, etc then you can actually just subtract id's to get the rows between ids, but I went ahead and counted rows anyway. The expression for calculating caux is long and I needed a function to calculate the first row in tblFreq where col1=1 anyway so I abstracted most everything to the function getCaux.
after adding the autonumber column ID go to the create tab on the ribbon and add a code module with the following
'Utilities
Private firstIDtblFreqcol1is1 As Long
Public Function getfirstIDtblFreqcol1is1() As Long
If firstIDtblFreqcol1is1 = 0 Then
firstIDtblFreqcol1is1 = DMin("ID", "tblFreq", "col1=1")
End If
getfirstIDtblFreqcol1is1 = firstIDtblFreqcol1is1
End Function
Public Function mostrecentrowwithcol1is1(id As Long) As Long
mostrecentrowwithcol1is1 = DMax("ID", "tblFreq", "ID < " & id & " AND col1 = 1")
End Function
'if col1=1 caux =1
'first row where col1=1 is 3 so if id < 3 caux is column count
'else caux = column count(id) - column count(id last row with col1=1)
Public Function getCaux(id As Long) As Long
If DLookup("col1", "tblFreq", "ID = " & id) = 1 Then
getCaux = 1
ElseIf id < getfirstIDtblFreqcol1is1 Then
getCaux = DCount("ID", "tblFreq", "ID <= " & id)
Else: getCaux = DCount("ID", "tblFreq", "ID <= " & id) - DCount("ID", "tblFreq", "ID <= " & mostrecentrowwithcol1is1(id)) + 1
End If
End Function
Then the query is just:
I have query 1 or table 1 have the below data
Number of employee/start date/end date/code/different date by Days/checkbox
Example
200/01-01-2021/15-01-2021/E/14/Yes
I need when checkbox=Yes to open 14 record automatically in new table 2 with code like below:
employee/date/code
200/01-01-2021/E
200/02-01-2021/E
200/03-01-2021/E
200/04-01-2021/E
200/05-01-2021/E
200/06-01-2021/E
200/07-01-2021/E
200/08-01-2021/E
200/09-01-2021/E
200/10-01-2021/E
200/11-01-2021/E
200/12-01-2021/E
200/13-01-2021/E
200/14-01-2021/E
Extract your parameters:
Data = "200/01-01-2021/15-01-2021/E/14/Yes"
ENo = Split(Data, "/")(0)
FirstDate = DateValue(Split(Data, "/")(1))
ECode = Split(Data, "/")(3)
Periods = Split(Data, "/")(4)
Then run this query passing it the parameters:
PARAMETERS
Periods Short,
FirstDate DateTime;
SELECT DISTINCT
10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10) + 1 AS Sequence,
ENo As Employee,
DateAdd("d", [Sequence] - 1, [FirstDate]) AS DateStart,
ECode As Code
FROM
MSysObjects AS Uno,
MSysObjects AS Deca
WHERE
(10 * Abs([Deca].[id] Mod 10) + Abs([Uno].[id] Mod 10)) < [Periods];
Output:
You can the use this query as source in an append query.
Or, alternatively, use DAO to directly append the records:
Dim Records As DAO.Recordset
Dim Index As Integer
Set Records = CurrentDb.OpenRecordset("Select * From Table2")
For Index = 0 To Periods - 1
Records.AddNew
Records!Employee.Value = Eno
Records!Date.Value = DateAdd("d", Index, FirstDate)
Records!Code.Value = ECode
Records.Update
Next
Records.Close
I have a MySQL database with a table named generations the structure of the table is as follows
I want to get value 10 as output when ten_generation have value 1 otherwise it will not return any value, 20 as output if twenty_generation have value 1 otherwise it will not return any value, 30 as output if thirty_generation have value 1 otherwise it will not return any value. If all the three fields has a value 1 output will be 10,20,30 also the task_id will provided as the input.
Its unclear what you intend the output to be when multiple generation columns are 1 but one solution is to use a CASE statement:
SELECT CASE
WHEN ten_generation = 1 THEN 10
WHEN twenty_generation = 1 THEN 20
WHEN thirty_generation = 1 THEN 30
ELSE NULL
END AS value
FROM generations
WHERE id = :your_id
If you want it as multiple columns then:
SELECT CASE
WHEN ten_generation = 1
THEN 10
ELSE NULL
END AS ten_value,
CASE
WHEN twenty_generation = 1
THEN 20
ELSE NULL
END AS twenty_value,
CASE
WHEN thirty_generation = 1
THEN 30
ELSE NULL
END AS thirty_value
FROM generations
WHERE id = :your_id
if only twenty_generation contain value 1 the output is 20 and if twenty_generation and ten_generation contain value 1 output is 10,20
Oracle Query:
SELECT TRIM(
LEADING ',' FROM
CASE WHEN ten_generation = 1 THEN '10' END
|| CASE WHEN twenty_generation = 1 THEN ',20' END
|| CASE WHEN thirty_generation = 1 THEN ',30' END
) AS value
FROM generations
WHERE id = :your_id
For MySQL you'd use CONCAT_WS:
select
concat_ws(',',
case when ten_generation = 1 then '10' end,
case when twenty_generation = 1 then '20' end,
case when thirty_generation = 1 then '30' end
) as result
from mytable
where task_id = 2;
I want to get the month name from a Do Until......Loop and display in a CheckedListBox.
I have two tables.
1. Month_Count
2. Fees_Ledger
My month name function...
Function mnthName(ByVal mnth As Integer)
Dim name As String = String.Empty
name = MonthName(mnth, False)
Return name
End Function
To get the Month Details
"SELECT FromMonth,ToMonth,MonthCount FROM Month_Count WHERE SemesterNumber='1'"
And to get the Paid Count
"SELECT COUNT(*) AS TotMonthPaidCount FROM Fees_Ledger WHERE SemYear='1' AND FeeId='1'"
So...
' Got the values from queries
FromMonth = 7 '(July)
ToMonth = 6 '(June)
MonthCount = 12 '(Loop will rotate 12 times)
TotMonthPaidCount = 1 '(FeeId 1 paid one time)
'Declaring an integer variable
Dim StartM As Integer = 1
FromMonth = FromMonth + TotMonthPaidCount
' For the first month
CheckedListBoxMonth.Items.Clear()
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
' Now the loop to achieve the goal
Do Until StartM = (MonthCount - TotMonthPaidCount)
If FromMonth >= 12 Then
FromMonth = 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
Else
FromMonth += 1
CheckedListBoxMonth.Items.Add(mnthName(FromMonth))
End If
StartM += 1
Loop
This Subroutine results exactly what I want.
But the problem occurs when the StartMonth = FromMonth (6) + TotMonthPaidCount (7) value >12. As 13 or 14 or 15 has no Month Name, it is showing error.
Argument 'Month' is not a valid value.
I want it like below.
What should I do ?
Is it possible to update the cell value only to the specific maximum value? Here is query:
UPDATE table_1 SET premium_photos = premium_photos + 2 WHERE number = '1234'
I want to limit premium_photos (tinyint) value to max value of 4. Is it possible? For example if premium_photos current value is 2 and query is + 3, then after this query value will be 4.
try
UPDATE table_1 SET
premium_photos = (CASE WHEN (premium_photos + 2) > 4 THEN 4 ELSE (premium_photos + 2) END)
WHERE number = '1234'
you can also use IF function
UPDATE table_1 SET
premium_photos = IF(premium_photos+2>4, 4, premium_photos+2)
WHERE number = '1234'
IF() function documentation