I have mysql database like this
id | code
1 | bok-1
2 | bok-2
3 | bok-3
4 | inv-1
5 | inv-2
6 | inv-3
How do i get latest number of prefix bok-?
code which from the example above the result will be bok-3
This works :
select code
from Table1
where code like "bok%"
order by cast(substring(code from 5) as signed) desc
limit 1
EDIT :
it now gives bok-11 if there is a bok-11 in the list, as I suppose 11 is greater than 3.
For the code try:
select code from table where id in (select max(id) where code like 'bok-%');
and for the id only:
select max(id) where code like 'bok-%'
Related
Blanking out on how to get this data:
I have a table like this:
====================
ID | Source
====================
1 | google
1 | fb
1 | linkedIn
1 | linkedIn
3 | linkedIn
3 | fb
I want to return all the Ids that have 3 sources.
So in the above table, I want the result set to contain only id 1 since it was hit across 3 sources.
Database is mysql 5.7 so anything functions on 8.0 only won't work. Thanks for any help!
Use aggregation:
select id
from mytable
group by id
having count(distinct source) = 3
Try a query like this and see if that is helpful. It should return only IDs that have exactly 3 unique sources.
SELECT ytn.id, COUNT(DISTINCT ytn.source) AS unique_sources
FROM `your_table_name` ytn
GROUP BY ytn.id
HAVING COUNT(DISTINCT ytn.source) = 3;
I have a table with some primary IDs inserted.
In anoter post i have already done i was provided half of the answer i requested and i am thankful for this. (MySQL select statement returning results in circle mode)
I tried to accomplish the other half with no luck. What i want to achieve is a select statement that will get me the opposite of the below example.
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table (id SERIAL PRIMARY KEY);
INSERT INTO my_table VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9);
The select statement i was provided with:
SELECT * FROM my_table ORDER BY id > 5 DESC, id;
Returns 6 - 7 - 8 - 9 - 1 - 2 - 3 - 4 - 5
I also need a select statement to return:
5 - 4 - 3 - 2 - 1 - 9 - 8 - 7 - 6
Thank you in advance!
You need conditional sorting:
SELECT * FROM my_table
ORDER BY id < 6 DESC, id DESC;
See the demo.
Results:
| id |
| --- |
| 5 |
| 4 |
| 3 |
| 2 |
| 1 |
| 9 |
| 8 |
| 7 |
| 6 |
From your earlier question, it seems you need ways to find the next and previous item in a database. This assumes
you know the id of the current item
the id is a primary key or other unique value
next gets the next higher id value
previous gets the next lower id value
when you get to either end of the range of id values, you get nothing back... there isn't any next or previous.
Here's how to get the next id.
SELECT id FROM tbl WHERE id > [[[current_id]]] ORDER BY id LIMIT 1
And, similarly, here's how to get the previous value.
SELECT id FROM tbl WHERE id < [[[current_id]]] ORDER BY ID desc LIMIT 1
These queries return just one row, or no rows if there's no next or previous
There's not much to be gained from a conditional ordering scheme when you only need a single value
I have to get result from column which name is generated by the data from another column. I will explain with the following example:
column names in the database:
months | am1 | am2 | am3 | am4 | am5 | am6 | am7 | am8 | am9 | am10 |am11 | am12
I want in my query to get the value from column starting with am + months value
my current query is:
$query = 'select id, iid, contractnumber, concat("am",`months`) as amount from credits where iid > 0';
but this instead of returning the value (71) of the specific am.. column it returns the column name, for example am5
How can I directly access the value of column am5
Thank you for your time !
That is a bad design and you should change it, but if that's what you got, you can use CASE
select id, iid, contractnumber,
case months
when 1 then am1
when 2 then am2
when 3 then am3
when 4 then am4
when 5 then am5
when 6 then am6
when 7 then am7
when 8 then am8
when 9 then am9
when 10 then am10
when 11 then am11
when 12 then am12
end as amount
from credits
where iid > 0
We have a table which contains card_no information. containing data like:
-----------------------------------------
| id [int(11)] | card_no [varchar(16)] |
-----------------------------------------
| 1 | 0124578965874563 |
| 2 | 1245789658478596 |
| 3 | 8471452369587458 |
-----------------------------------------
Now we need a query to find card number(s) which contains 7 in 6th position. Or which contains 4 in 2nd position.
This is actually needed when we printed card numbers and find some numbers unreadable. so we need to identify the card with rest of the numbers. For example we have data like:
1245_896584_8596
Now we need to identify the card with this data.
Thanks in advance.
You can use function SUBSTRING:
SELECT id, card_no
FROM mytable
WHERE SUBSTRING(card_no, 6, 1) = '7' OR SUBSTRING(card_no, 2, 1) = '4'
Demo here
Use SUBSTR string function
SELECT *
FROM yourtable
WHERE SUBSTR(card_no,2,1) = 4
OR SUBSTR(card_no,6,1) = 7
Use like in where clause and wildcard for exactly one symbol _
Something like
select * from table where card_no like '_____7℅'
I have a mysql table that looks something like this:
id | PO | DAP | MEDIA
---|----|-------|------
1 | 2 | 34 | 64
2 | 6 | 53 | 23
I would like to be able to query get multiple rows, one for each column. E.g:
SELECT column_name as column, column_value as value FROM my_table;
Which would give me:
PO=2,DAP=34,MEDIA=54,PO=6,DAP=53,MEDIA=23
What would I need to use to formulate a query like this?
You have to first CONCAT the data of each specified field and apply GROUP_CONCAT ON the result.
Query
SELECT GROUP_CONCAT(temp_col) FROM
(
SELECT 1 as 'temp_id',
CONCAT(
CONCAT('PO=', PO),
',',
CONCAT('DAP=', DAP),
',',
CONCAT('MEDIA=', MEDIA)
) AS 'temp_col'
FROM test
) temp
GROUP BY temp_id
Check Out SQLFIDDLE
Not exactly sure what you mean. But this is traditionally done in this manner
SELECT * FROM my_table;
You'll get your array like this
array(0=>array('PO'=>2,'DAP'=>34,'MEDIA'=54), 1=>array('PO'=>6, 'DAP'=>53, 'MEDIA'=> 23))
.. like so.