I have created 2 temporary tables:
-------------- ------------------
| withlabfee | | without lab fee|
-------------- ------------------
| 6 | | 3 |
-------------- ------------------
My code returned the following result with single column.
-------------------------------
| withlabfee , without lab fee|
-------------------------------
| 6 , 3 |
------------------------------
My attempted code as below
drop table if exists withlabFee;
create temporary table withlabFee(labFee1 int);
insert into withlabFee
select count(*)
from course_relation
where lab_fee is not null;
drop table if exists withoutLabfee;
create temporary table withoutLabfee(labFee2 int);
insert into withoutLabfee
select count(*)
from course_relation
where lab_fee is null;
select concat(labfee1,',',labfee2) as `With lab fee , Without lab fee`
from withlabFee, withoutLabfee;
drop table labFee;
drop table withoutLabfee;
How do i combine the column from my temp table withlabfee and withoutlabfee into 1 table with 2 columns like below:
-------------------------------
| withlabfee | without lab fee|
-------------------------------
| 6 | 3 |
-------------------------------
Any helps are very much appreciated!
Solution even without temporary tables:
SELECT
SUM(lab_fee IS NOT NULL) AS 'With lab fee',
SUM(lab_fee IS NULL) AS 'Without lab fee'
FROM
course_relation
Try the below query (without creating temp tables).
SELECT
COUNT(CASE WHEN lab_fee IS NOT NULL THEN 1 END) AS withlabFee,
COUNT(CASE WHEN lab_fee IS NULL THEN 1 END) AS withoutLabfee
FROM course_relation
Related
In MySQL I need to alter a table to combine 2 rows if they are the same and have it add the qty column together. I don't want to create a new table just alter the one that's there for example if I have a row that is:
Account
Name
Item
Qty
123
John
Red Shoe
1
123
John
Red Shoe
1
How would I alter the table to combine the two rows to be:
Account
Name
Item
Qty
123
John
Red Shoe
2
I cannot find the answer to this kind of question anywhere online.
It isn't clear what you are asking for. Alter Table is not appropriate and there is no mysql function which can combine rows or any statement which could insert new rows and delete other rows - you would need an insert followed by a delete..but the delete would have to know what to delete and it's not obvious from your question how this could be achieved
DROP table if exists t;
create table t
(Account int,Name varchar(20), Item varchar(20) ,Qty int);
insert into t values
(123 ,'John' ,'Red Shoe' ,1),
(123 ,'John' ,'Red Shoe' ,1);
#select * from t;
insert into t
select account,name,item,sum(qty)
from t
group by account,name,item;
select * from t;
---------+------+----------+------+
| Account | Name | Item | Qty |
+---------+------+----------+------+
| 123 | John | Red Shoe | 1 |
| 123 | John | Red Shoe | 1 |
| 123 | John | Red Shoe | 2 |
+---------+------+----------+------+
3 rows in set (0.001 sec)
I have a question and don't know how to approach the problem exactly.
I have two tables as following:
Clients
| c_id | name | reference |
| ---- | ------- | --------- |
| 1 | ClientA | 1 |
| 2 | ClientB | 1 |
| 3 | ClientC | 2 |
| 4 | ClientD | 2 |
| 5 | ClientE | 1 |
| 1 | ClientF | 3 |
Tour
| t_id | name | count |
| ---- | ------- | ----- |
| 1 | TourA | 3 |
| 2 | TourB | 2 |
| 3 | TourC | 1 |
"Reference" in the "Client" table is defined as foreign key.
Is it possible to fill the column "count" in the table "Tour" with an automated formula where it counts how many times the t_id appears in the "Client" table?
Something like: COUNT(c_id) FROM clients WHERE reference = t_id
I have read about to create a view but not sure how to fetch the data correctly.
Thanks for your help,
Raphael
UPDATE #1:
The workflow as described with the view works perfectly. I'm trying now to fill the column via a trigger but I'm getting an SQL error with the following code:
CREATE TRIGGER client_count
AFTER UPDATE
ON clients FOR EACH ROW
SELECT t.*,
(
SELECT COUNT(*) FROM clients c where c.tour_id = t.tour_id
) AS tours.tour_bookedspace
FROM tours t
The view you have referred to is indeed the way to go here. The view you need to create needs to join the two tables and perform a count aggregation as follows:
CREATE VIEW vwTour
AS
SELECT t.t_id,
t.name,
COUNT(t.name) AS Cnt
FROM tour t
JOIN Clients c
ON t.t_id = c.reference
GROUP BY t_id,
t.name
No you can't. Generated columns can only use data from the same table.
The options you have are:
1. Use a view
You can select from a view that computes the extra value(s) you want. For example:
create view tour_data as
select t.*,
(
select count(*) from clients c where c.reference = t.t_id
) as number_of_clients
from your t
2. Use a trigger
Alternatively, you can add the extra column number_of_clients and populate it using a trigger every time a row is added, modified, or deleted from the table clients.
I have a table in MySQL which is of the following form
ID Team Task
-----------------
1 Team01 Task_01
2 Team02 Task_02
3 Team02 Task_01
The values in the Team and Task column are repetitive.
I need to collect the distinct tasks from the table and use it to create a new table whose schema would be of the form Table (Task_01 varchar(20),Task_02 varchar(20));
Table (Team varchar(15) PRIMARY KEY, Task_01 int, Task_02 int)
Upon creation , I need to populate this new table with frequency of the tasks performed by respective teams.
Need some directions to proceed ahead. Thanks.
[EDIT]
The expected output is another table as shown below
Team | Task_01 | Task_02
Team01 | 1 | 0
Team02 | 1 | 1
Just need help/ideas on how to create the schema(via SQL Scripts only) once all distinct tasks are obtained from the given table.
Given this sample data:
CREATE TABLE Table1
(`ID` int, `Team` varchar(6), `Task` varchar(7))
;
INSERT INTO Table1
(`ID`, `Team`, `Task`)
VALUES
(1, 'Team01', 'Task_01'),
(2, 'Team02', 'Task_02'),
(3, 'Team02', 'Task_01')
;
You would usually just do this
SELECT
Team, Task, COUNT(*)
FROM Table1
GROUP BY Team, Task;
to get this result
| TEAM | TASK | COUNT(*) |
|--------|---------|----------|
| Team01 | Task_01 | 1 |
| Team02 | Task_01 | 1 |
| Team02 | Task_02 | 1 |
which is not in the desired format, but usually formatting is done in application layer. So this is what I recommend. It's easy and performant and has no drawbacks like what do you do when you want to add another task? You would add another column. When a team has nothing to do with this task, it has NULL values in this column. This is bad. Anyway...if you absolutely want to have your output like in the question and not do it on application level, you can do it like this:
SELECT
Team,
COUNT(IF(Task = 'Task_01', 1, NULL)) AS Task_01,
COUNT(IF(Task = 'Task_02', 1, NULL)) AS Task_02
FROM Table1 t
GROUP BY Team
Result:
| TEAM | TASK_01 | TASK_02 |
|--------|---------|---------|
| Team01 | 1 | 0 |
| Team02 | 1 | 1 |
I have table containing following entries
Id | Accno | Name | Hash
----+----------+-----------+---------
1 | 11 | ABC | 01110
2 | 11 | ABC |
3 | 22 | PQT |
4 | 33 | XYZ | 03330
5 | 44 | LMN | 04440
6 | 33 | XYZ |
I need SQL query to remove duplicate entry from table and keep atleast single entry in table whose hash value is present. and for those entries which are not duplicate should also remain in table.
I think you guys overcomplicate things a lot. This should work just dandy:
DELETE FROM
YourTable
WHERE Hash IS NULL
AND Accno IN
(
SELECT Accno
FROM YourTable
GROUP BY Accno
HAVING COUNT(Name) > 1
)
;
Probably the easiest way to do it is to create a new table and copy non duplicate entries.
create table table_name2 as select distinct * from table_name1;
drop table table_name1;
rename table_name2 to table_name1;
Something like this.
Create table temp2 as SELECT *
FROM temp where id in (select id from temp group by accno having count(accno)>=1 and hash<>'');
drop table old_table;
rename table temp2 to old_table;
Check SQL Fiddle
I didn't see this exact question asked. Most people seem to want to sync. I just need a one-time copy.
MySQL version is 5.5.35.
In my MySQL database I need to one-time copy data from TableA:FieldA to TableB:FieldB while matching a common UID field — TableA:UID and TableB:UID are the related fields.
More specifically I am copying Employee ID from one table to a different field in another table, and both tables have Contact ID in common. So obviously I need TableA:UID=1's Employee Number to appear in TableB on the correct row where TableB:UID=1.
Thanks.
UPDATED: Tested the solution, got an error 1442
UPDATE civicrm_value_member_fields_1
SET civicrm_value_member_fields_1.aft_id_43 =
(SELECT civicrm_contact.external_identifier FROM civicrm_contact
WHERE civicrm_contact.id = civicrm_value_member_fields_1.entity_id)
alt version of the above:
UPDATE `civicrm_value_member_fields_1`
SET `aft_id_43` =
(SELECT `external_identifier` FROM `civicrm_contact`
WHERE `id` = `entity_id`)
both error 1442:
#1442 - Can't update table 'civicrm_contact' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
The following example should help.
create table A(id int,cid int);
create table B(id int,cid int);
insert into A values(6,1);
insert into A values(7,2);
insert into A values(8,3);
insert into A values(9,4);
insert into B(cid) values(1);
insert into B(cid) values(3);
insert into B(cid) values(4);
Table A
| ID | CID |
|----|-----|
| 6 | 1 |
| 7 | 2 |
| 8 | 3 |
| 9 | 4 |
Table B
| ID | CID |
|--------|-----|
| (null) | 1 |
| (null) | 3 |
| (null) | 4 |
The UPDATE query will update B's id field referring A's id field.
update B set B.id = (select A.id from A where A.cid = B.cid);
Table B
| ID | CID |
|----|-----|
| 6 | 1 |
| 8 | 3 |
| 9 | 4 |
The error can be sidestepped by first creating a temporary table - it will have all the data you need but not have the functions/triggers that impede the step.
Also, once the table is set then a JOIN statement is a more efficient representation of the action and should run more quickly as well.
CREATE TEMPORARY TABLE contact_dupe SELECT id, external_identifier FROM civicrm_contact;
UPDATE civicrm_value_member_fields_1 AS cs
JOIN contact_dupe AS cd ON cd.id=cs.entity_id
SET cs.aft_id_43=cc.external_identifier;