I'm new to SQL, I have a table, in that table column have following data
/message/charge/392bd2c574-90e7-da9f70ed01b2/text
I executed my query
select message from emp_data;
output:
/message/charge/392bd2c574-90e7-da9f70ed01b2/text
but I want only
392bd2c574-90e7-da9f70ed01b2
value in output so how to split it through SQL query
try substring_index function-
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('/message/charge/392bd2c574-90e7-da9f70ed01b2/text','/',-2),'/',1);
You can use below query including your field and table-
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(`message`,'/',-2),'/',1) FROM emp_data;
Note: Further you can add other fields and where condition as per your requirement.
Related
Essentially I have a table in my database called Table1 with the following data:
The table has a ProductID that repeats because the values of AssignedColour, ColourFinding and ColourPower vary.
I would like to present all ProductID data in one single row, meaning if there is more than one AssignedColour, ColourFinding and ColourPower listed, it will contain a number at the end.
The final result I of the SELECT query should look like the following:
The number of columns presented horizontally is based on the number of AssignedColour per ProductID
Is something like this possible to accomplish in a mySQL SELECT Query?
An SQL query cannot expand the number of columns of the result set depending on the data values it discovers during query execution. The columns in the SELECT-list must be fixed at the time the query is prepared, before it reads any data.
Also the column names cannot be changed during the query execution. They must be set at the time the query is prepared.
There's no way to do what you are describing in a single SQL query. Your options are:
Do two queries: one to enumerate the colors per product, and then use the result of the first to format a second query with the columns you want.
Do one query to fetch the data in rows as it exists in your table, then write code in your app to display it in rows however you think is best.
Either way, you have to write at least a bit of code in the client. You can't do this in one query.
Here I am retrieving user id in Mysql from two rows based onid_no so it will retrieve two separate rows but after that I Want that result should come in single row with comma separated.
query to get records:
SELECT uid FROM `users` WHERE id_no='f5e3fe73a8';
output:
but expected output
uid=2,1
so how to do this using mysql query?
use group_concat
SELECT group_concat(uid) FROM `users` WHERE id_no='f5e3fe73a8' Group by id_no
For more informations see the official documentation
I am using stored procedure. I am passing some input parameter to stored procedure like p_type. Based on this condition i need to get either 1 column or 3 column as output
like
select
a_orderid AS OrderId,
t_logisticpartner.a_name AS Logistics,
t_shipment.a_shipmentid AS ShipmentId
from order some join comitions;
If p_type is 1 then i need all 3 columns or else just orderid column. I need to have aliases also.
Two MySQL solutions:
Write big switch with all possible SELECT queries using CASE or IF statements.
Construct result SELECT statement, and then execute it using MySQL prepared statements.
Is there a way in MySql to use an either or in a select column. For instance
select left(columnA,2) or right(columnA,1) as columnAlias, sum(columnB)
from table
where ((left(columnA,2) in ('aa','bb','cc')) or (right(columnA,1) in ('a,','b','c')))
group by columnAlias
what I have is a table where either the first 2 characters of the column or the last character of the column indicates the facility. I need to sum the values by facility. A union gets me part way there then I could loop through the resulting dataset and sum things up in the code (or do a stored proc to return the sums), but I am wondering if there is a way to just get it from the query.
I've tried using the union query as an on the fly temp table and doing the select and group on that but if there are no records returned from either of the select statments then it throws a "column columnA cannot be null error.
Also tried with the syntax above, but not getting the results I am expecting. Any other ways to do this through the query?
using a CASE would prob be your best bet here.
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html
I am having a sql query
select devices.id, devices.type_designator_id, devices.color, devices.status,
devices.device_build, users.username
from devices,users
where
devices.user_id=users.id and devices.user_id=1608
ORDER BY devices.id;
Now it will give me 6 output from two tables devices and users.
Now I want to extract only one output from above query (without changing the anything) type_designator_id, to put it as a parameter for next sql query with different table.
Say new table is Type_designators with a parameter name id, which is same as the type_designator_id from the previous query.
You could consider creating a view defined by the query you've shown above, and using that view in your new query.