Related
I've got the following tables in my database:
this is the table km_kondomanager_millesimal_table_values
this is the table km_kondomanager_millesimal_table
The two tables are joined together by km_kondomanager_millesimal_table_value_table_id and km_kondomanager_millesimal_table_id
I've managed to creat a pivot table using the followinf query:
SELECT km_kondomanager_millesimal_table_value_building_id
,SUM(CASE WHEN km_kondomanager_millesimal_table_value_table_id = 9
THEN km_kondomanager_millesimal_table_millesimal_value END) tabella_gruppo_A
,SUM(CASE WHEN km_kondomanager_millesimal_table_value_table_id = 10
THEN km_kondomanager_millesimal_table_millesimal_value END) tabella_gruppo_B
FROM km_kondomanager_millesimal_table_values
WHERE km_kondomanager_millesimal_table_value_group_id = 15
GROUP BY km_kondomanager_millesimal_table_value_building_id
and this is how it looks like:
My problem is that I need to take the table name from km_kondomanager_millesimal_table and use it as column, these columns are not always the same as you can see in the immage there are other stored in the database. The query I've created requires to specify km_kondomanager_millesimal_table_value_table_id in the case and also to specify the column name in the example tabella_gruppo_A and tabella_gruppo_B. Can anyboby help me to achieve this? Also I need to convert the pit table to HTML table. Many thanks
I've managed to get it sorted using mysql stored procedure and this is the code:
BEGIN
SELECT
GROUP_CONCAT(
CONCAT("MAX(IF(km_kondomanager_millesimal_table_value_table_id='", km_kondomanager_millesimal_table_value_table_id, "',km_kondomanager_millesimal_table_millesimal_value ,NULL)) AS '", km_kondomanager_millesimal_table_name, "'"), "
"
)INTO #answers
FROM (
SELECT DISTINCT km_kondomanager_millesimal_table_value_table_id, km_kondomanager_millesimal_table_name FROM km_kondomanager_millesimal_table_values INNER JOIN km_kondomanager_millesimal_table
ON km_kondomanager_millesimal_table_values . km_kondomanager_millesimal_table_value_table_id = km_kondomanager_millesimal_table. km_kondomanager_millesimal_table_id WHERE km_kondomanager_millesimal_table_value_group_id = 13
) A;
SET #query :=
CONCAT(
'SELECT km_kondomanager_millesimal_table_value_building_id, ', #answers,
' FROM km_kondomanager_millesimal_table_values WHERE km_kondomanager_millesimal_table_value_group_id = 13 GROUP BY km_kondomanager_millesimal_table_value_building_id'
);
PREPARE statement FROM #query;
EXECUTE statement;
END
How i can get value like this in a variable 'TFSEP-2019','TFjul-2018','TFJun-2018' without spaces.
SELECT s.house, s.grade, s.homeroom AS Campus
FROM student s,fees_jrl f
WHERE s.studnum = f.studnum AND
f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
GROUP BY s.house
I am getting like this ('TFJun-2018 TFJUL-2018 TFSEP-2019') but I want like ('TFSEP-2019','TFjul-2018','TFJun-2018') please help
You may refer the following statements with like operators, whenever you need quotes('), use doubled-quotes('') :
create table tab ( Id int, expression varchar(100));
insert into tab values(1 ,'TFJun-2018 TFJUL-2018 TFSEP-2019');
insert into tab values(2,'''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''');
select * from tab where expression like '''TFJun-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
Rextester Demo
You can use like for strings
% means any chars, _ means one char
So doing
F.name like %2018%
Will give you all of 2018..
use explicit join and concat
SELECT
concat( concat( concat( concat("'",s.house),"'"),concat( concat("'",s.grade),"'")),
concat( concat("'",s.homeroom),"'"))
FROM student s join fees_jrl f
on s.studnum = f.studnum
where f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
I think you dont need group by as you have no aggregation function
I am trying to solve an issue with joining last and first names that are identified using id's in another table. My code is producing the correct fields, but the Guide_Name and Guest_Name columns show all/only 0 (zero). Here is my code:
use www;
SELECT
d.destination_name,
tt.trip_type_name,
t.trip_number,
t.trip_date,
CONCAT(e.last_name + ', ' + e.first_name) AS guide_name,
CONCAT(g.last_name + ', ' + g.first_name) AS guest_name,
ex.exp_name AS guest_experience,
g.age AS guest_age,
g.weight AS guest_weight,
g.swimmer AS guest_is_swimmer,
g.mobile_phone AS guest_mobile_phone
FROM
trip_type tt
JOIN
trips t ON tt.trip_type_code = t.trip_type_code
JOIN
destination d ON t.destination_code = d.destination_code
JOIN
reservation r ON t.trip_number = r.trip_number
JOIN
guests g ON r.guest_id = g.guest_id
JOIN
experience ex ON ex.exp_code = g.exp_code
JOIN
employees e ON t.guide_employee_id = e.employee_id
ORDER BY d.destination_name , tt.trip_type_name , t.trip_date , g.last_name , e.employee_id
And here is the EER diagram:
CONCAT should just be a comma separated list of strings, so I would first change
CONCAT(e.last_name + ', ' + e.first_name)
to
CONCAT(e.last_name, ', ', e.first_name)
and see if that helps.
With + in the concat, mysql thinks you want them treated as numbers. Perhaps you're confusing it with javascript?
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
dave has the heart of this problem, but I expect you also will want to deal with nulls. I think the final code you want will look like this:
COALESCE(CONCAT(e.last_name,', ',e.first_name),e.last_name,e.first_name,'') AS guide_name,
COALESCE(CONCAT(g.last_name,', ',g.first_name),g.last_name,g.first_name,'') AS guest_name,
There are two columns in a MySQL table: SUBJECT and YEAR.
I want to generate an alphanumeric unique number which holds the concatenated data from SUBJECT and YEAR.
How can I do this? Is it possible to use a simple operator like +?
You can use the CONCAT function like this:
SELECT CONCAT(`SUBJECT`, ' ', `YEAR`) FROM `table`
Update:
To get that result you can try this:
SET #rn := 0;
SELECT CONCAT(`SUBJECT`,'-',`YEAR`,'-',LPAD(#rn := #rn+1,3,'0'))
FROM `table`
You can use mysql built in CONCAT() for this.
SELECT CONCAT(`name`, ' ', `email`) as password_email FROM `table`;
change field name as your requirement
then the result is
and if you want to concat same field using other field which same then
SELECT filed1 as category,filed2 as item, GROUP_CONCAT(CAST(filed2 as CHAR)) as item_name FROM `table` group by filed1
then this is output
In php, we have two option to concatenate table columns.
First Option using Query
In query, CONCAT keyword used to concatenate two columns
SELECT CONCAT(`SUBJECT`,'_', `YEAR`) AS subject_year FROM `table_name`;
Second Option using symbol ( . )
After fetch the data from database table, assign the values to variable, then using ( . ) Symbol and concatenate the values
$subject = $row['SUBJECT'];
$year = $row['YEAR'];
$subject_year = $subject . "_" . $year;
Instead of underscore( _ ) , we will use the spaces, comma, letters,numbers..etc
In query, CONCAT_WS() function.
This function not only add multiple string values and makes them a single string value. It also let you define separator ( ” “, ” , “, ” – “,” _ “, etc.).
Syntax –
CONCAT_WS( SEPERATOR, column1, column2, ... )
Example
SELECT
topic,
CONCAT_WS( " ", subject, year ) AS subject_year
FROM table
I have two columns:
prenom and nom so to concatenate into a column with name chauffeur_sortant I used this script:
SELECT date as depart, retour, duree_mission, duree_utilisation, difference, observation, concat( tb_chaufeur_sortant.prenom, ' ', tb_chaufeur_sortant.nom) as chauffeur_sortant, concat(tb_chaufeur_entrant.prenom, ' ', tb_chaufeur_entrant.nom) as chauffeur_entrant
FROM tb_passation
INNER JOIN tb_vehicule
ON tb_vehicule.id = tb_passation.id_vehicule
INNER JOIN tb_chaufeur_sortant
ON tb_chaufeur_sortant.id = tb_passation.id_sortant
INNER JOIN tb_chaufeur_entrant
ON tb_chaufeur_entrant.id = tb_passation.id_entrant WHERE tb_vehicule.id = '';
$crud->set_relation('id','students','{first_name} {last_name}');
$crud->display_as('student_id','Students Name');
I have and ORDERS table from a customer shopping cart. It's the typical customer name, address, etc..list of fields. The twist is that the cart will populate the shipping information into the appropriate fields (shipName, shipAddress, ShipState, etc) when the customer enters shipping information that is different then the billing information. However, when the customer ships to themselves, the cart does not move the shipping information to the proper fields for shipping (i.e., shipName, shipAddress, shipState). It keeps this information in the ordName, ordAddress, ordCity, etc fields and then keeps the shipping related fields empty.
Therefore, these shipping fields are then empty for orders that customers ship to themselves. I used IF commands to move the ordName, ordAddr, etc to alias names when no shipping information is provided so that the shipping information for all order types (whether the customer ships to themselves or to another address) is handled. That part is working fine in my query below.
One issue remains. My shipping program can't use long State name (e.g, Michigan, New York, etc). It needs the state to be the two character abbreviation (e.g, MI, NY). I have a look up table called *STATES that has a mapping between long state name and the two character abbreviation. I am trying to use the ShipState alias to look up the correct two character name for a given state. I tried to do this as a JOIN but keep getting errors. I removed the join I was using and am only showing the code that works correct right now but doesn't do the mapping for state abbreviation. Can someone please help?
SELECT
orders.ordDate AS `Date`,
orders.ordID AS Order_ID,
orders.ordEmail AS Email,
IF(ordShipName = ' ', ordName, ordShipName) AS Name,
IF(ordShipAddress = ' ', ordAddress, ordShipAddress) AS Address_1,
IF(ordShipAddress2 = ' ', ordAddress2, ordShipAddress2) AS Address_2,
IF(ordShipCity = ' ', ordCity, ordShipCity) AS City,
IF(ordShipState = ' ', ordState, ordShipState) AS ShipState,
IF(ordShipZip = ' ', ordZip, ordShipZip) AS Postal,
IF(ordShipCountry = ' ', ordCountry, ordShipCountry) AS Country,
IF(ordShipPhone = ' ', ordPhone, ordShipPhone) AS Phone,
FROM
orders
WHERE
orders.ordID > 21700
HAVING
Country = 'United States of America'
You can't use the column alias ShipState for the join to your lookup table. Instead, repeat your IF construct again:
SELECT
orders.ordDate AS `Date`,
orders.ordID AS Order_ID,
orders.ordEmail AS Email,
IF(ordShipName = ' ', ordName, ordShipName) AS Name,
IF(ordShipAddress = ' ', ordAddress, ordShipAddress) AS Address_1,
IF(ordShipAddress2 = ' ', ordAddress2, ordShipAddress2) AS Address_2,
IF(ordShipCity = ' ', ordCity, ordShipCity) AS City,
IF(ordShipState = ' ', ordState, ordShipState) AS ShipState,
IF(ordShipZip = ' ', ordZip, ordShipZip) AS Postal,
IF(ordShipCountry = ' ', ordCountry, ordShipCountry) AS Country,
IF(ordShipPhone = ' ', ordPhone, ordShipPhone) AS Phone,
STATE.StateAbbreviation
FROM
orders
INNER JOIN STATES
ON IF(orders.ordShipState = ' ', orders.ordState, orders.ordShipState) = STATES.State
WHERE
orders.ordID > 21700
HAVING
Country = 'United States of America'
You should put the code in where you try to join the two tables. You might just have a syntax error that someone could help you with. If you have a table called STATES with columns state and state_abrev, try this:
select ..., .., ..., STATES.state_abrev, ..., ..., from orders, STATES where STATES.state = orders.ordShipState AND ... ... HAVING ...
Since the origin of the data is a small set of transaction it will be very fast, I would have it do a left join to two instances of the states table respectively on their field and have your SELECT based on whichever was not empty... such as
select
...(other fields),
IF(orders.ordShipState = ' ', s1.StateName, s2.StateName ) as StateName
from
Orders
left outer join states s1
on orders.ordstate = s1.state
left outer join states s2
on orders.ordShipState = s2.state
where
orders.ordID > 21700
(etc with rest of query)...
This way, the join will always be having a join based on BOTH POSSIBLE states... So, whichever one exists, based on your preference, get THAT state first, otherwise get the other.