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
Related
Goal: I want to create a Select query where the result contains all records from both tables, except the time slot. In a addition to this I want to have the condition that if the minutes of parking are 0 or Null the value of the field should be set to -1.
Progress: At the current state I merged the two tables and could set the 0 value to -1. Due to the fact that I am quite new to SQL I couldn´t find a solution for keeping the original values for minutes and integrate the 'When Null Then -1' clause. Many solutions suggest a Update query , but the operation needs to be in a Select result. MYSQL 2017. This is my code so far:
Select c.ID, c.status, c.Date, Case When c.Minutes = 0 Then -1 End as Minutes
From Customer_1 as c
Union
Select c1.ID, c1.status, c1.Date, Case When c1.Minutes = 0 Then -1 End as
Minutes
From Customer_2 as c1
Original Dataset: I Have two tables with the exact same column names, representing user IDs
Customer_1:
ID| Date| Minutes| Time| status
1 | 2019| 3 | 2019| A
2 | 2019| 0 | 2019| A
Customer_2:
ID| Date| Minutes| Time| status
3 | 2019| Null | 2019| A
4 | 2019| 0 | 2019| A
What the final query should look like:
ID| Date| Minutes| status
1 | 2019| 3 | A
2 | 2019| -1 | A
3 | 2019| -1 | A
4 | 2019| -1 | A
Any suggestion how build a working query that fulfills the criteria would be much appreciated!
Just using Coalesce() function and adding Else part is needed within your Case .. When statement :
Select ID, status, Date, Case When Coalesce(Minutes,0) = 0 Then -1 Else Minutes End as Minutes
From Customer_1
Union
Select ID, status, Date, Case When Coalesce(Minutes,0) = 0 Then -1 Else Minutes End
From Customer_2
and using aliases for tables is redundant in this case, since the queries are independent except for Union. The alias(Minutes) for the second query is also redundant.
Another alternative might be using an IF statement along with COALESCE() function :
Select ID, status, Date, IF(Coalesce(Minutes,0) , Minutes, -1) as Minutes
From Customer_1
Union
Select ID, status, Date, IF(Coalesce(Minutes,0) , Minutes, -1)
From Customer_2
Demo
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 2 tables in my DB and I want to compare values of 2 select queries Ive made on each one
Table 1: click_log
Query table 1:
SELECT *
FROM click_log
Table 2: km_articles
Query table 2:
SELECT km_article_no
FROM km_articles
WHERE km_article_date <= "2017-10-31" AND km_article_status = "Published" AND km_article_view_count <= "5"
The columns I want to compare are table link_clicked for table 1 with km_article_no and I know I will find repeated matched, nevertheless from those repeated matches I want to find the latest one that I want to get from another column in table 1 called "when_clicked" that contains data information, not sure How can i put together those to queries and then narrow them down.
this is how the tables look like:
Table 1:
|link_clicked|when_clicked
KB00001 | 2017-08-02
KB00001 | 2017-12-02
KB00002 | 2017-08-02
KB00002 | 2017-09-02
KB00003 | 2017-09-02
KB00003 | 2017-09-02
Table 2:
km_article_no|km_article_ti|km_article_status|km_article_view_count|km_article_date
KB00001 |outlook IOS | Published | 5 | 2017-01-02
KB00002 |outlook CSS | Published | 4 | 2017-01-05
KB00003 |outlook ZTE | Retired | 3 | 2017-01-09
If I understand correctly, you want to show all km_articlesrows, each with the latest related click_log.when_clicked date. So aggregate your click_log per link_clicked and find the maximum when_clicked. Then join this to km_articles.
select kma.*, cl.last_clicked
from km_articles kma
join
(
select link_clicked, max(when_clicked) as last_clicked
from click_log
group by link_clicked
) cl on cl.link_clicked = kma.km_article_no
where kma.km_article_date <= date '2017-10-31'
and kma.km_article_status = 'Published'
and kma.km_article_view_count <= 5;
(If you also want to show km_articles rows that have no match in click_log, then change join to left join.)
Looking for a query that takes the following table ProductList
id| column_1 | column_2 | Sum
================================
1 | Product-A | Product-B | 67
2 | Product-A | Product-C | 55
3 | Product-A | Product-D | 23
4 | Product-B | Product-C | 95
5 | Product-C | Product-D | 110
and returns the first record Product-A_Product-B and then skips all records that contain Product-A or Product-B in either column and returns Product-C_Product-D.
I only want to return the row if everything in the row is appearing for the first time.
Assuming that the products don't contain ,, you could use a comma-delimited session variable to store already selected products and check for every row if one of the columns is already contained in that variable:
select column_1, column_2
from (
select l.*,
case when find_in_set(l.column_1, #products) or find_in_set(l.column_2, #products)
then 1
else (#products := concat(#products, ',', l.column_1, ',', l.column_2)) = ''
end as skip
from ProductList l
cross join (select #products := '') init
order by l.id
) t
where skip = 0;
Demo: http://rextester.com/NDVBW87988
But you should know the risks:
ORDER BY in a subquery is not really valid and usually doesn't make sence. The engine may skip it or move it to the outer query.
If you read and write the same session variable in one statement, the execution order is not defined. So the query might not work for all (future) versions.
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℅'