I want to make it in such a way that I get:
Main | Column I want to reset on new main value
1 | 100
1 | 101
1 | 102
1 | 103
1 | 104
2 | 100
2 | 101
You can use MyIsam engine to achieve this -
CREATE TABLE table1(
id1 INT(11) NOT NULL,
id2 INT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id1, id2)
);
INSERT INTO table1 VALUES (1, NULL);
INSERT INTO table1 VALUES (1, NULL);
INSERT INTO table1 VALUES (2, NULL);
INSERT INTO table1 VALUES (2, NULL);
SELECT * FROM table1;
+-----+-----+
| id1 | id2 |
+-----+-----+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
+-----+-----+
Related
I have a table people, a table departments and a table addresses.
I am wondering if there is a way in mySql to have some of the results as JSON rather than added rows or tables.
For this example:
people:
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| departmentId | int | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
addresses
+-------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| personId | int | YES | | NULL | |
| fullAddress | varchar(125) | YES | | NULL | |
+-------------+--------------+------+-----+---------+-------+
departments;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
Note: to create tables, run:
create table people ( id INT(11), name varchar(50), departmentId INT(11));
create table departments ( id INT(11), name varchar(50));
create table addresses ( id INT(11), personId INT(11), fullAddress varchar(125));
So a person can be linked to one specific department, but can have several addresses.
insert into departments values (0, 'sales');
insert into departments values (1, 'dev');
insert into people values (0, 'Chiara', 0);
insert into people values (1, 'Geoff', 0);
insert into people values (2, 'Tony', 1);
insert into people values (3, 'Raphael', 1);
insert into addresses values (0, 0, 'Street 0');
insert into addresses values (1, 0, 'Street 1');
insert into addresses values (2, 1, 'Street 2');
insert into addresses values (3, 1, 'Street 3');
insert into addresses values (4, 2, 'Street 4');
insert into addresses values (5, 2, 'Street 5');
insert into addresses values (6, 2, 'Street 6');
insert into addresses values (7, 2, 'Street 7');
insert into addresses values (8, 3, 'Street 8');
insert into addresses values (9, 3, 'Street 9');
QUESTION 1: JOIN 1:n as array rather than as multiple results
I can easily do a select and get all records in people:
select * from people left join addresses on people.id = addresses.personId where people.id = 2;
The result of this is:
+------+------+--------------+------+----------+-------------+
| id | name | departmentId | id | personId | fullAddress |
+------+------+--------------+------+----------+-------------+
| 2 | Tony | 1 | 7 | 2 | Street 7 |
| 2 | Tony | 1 | 6 | 2 | Street 6 |
| 2 | Tony | 1 | 5 | 2 | Street 5 |
| 2 | Tony | 1 | 4 | 2 | Street 4 |
+------+------+--------------+------+----------+-------------+
Is there an easy, straightforward way to have only ONE record as result (the contact ID 2) and have the addresses in an ARRAY (or, a JSON containing the addresses) instead?
QUESTION 2: JOIN 1:1 as a sub-object rather than adding columns
I can easily get the department information by using joins:
select * from people left join departments on departments.id = people.departmentId where people.id = 2 ;
Result:
+------+------+--------------+------+------+
| id | name | departmentId | id | name |
+------+------+--------------+------+------+
| 2 | Tony | 1 | 1 | dev |
+------+------+--------------+------+------+
However, what if I wanted a result like this instead:
+------+------+--------------+----------------------+
| id | name | departmentId | departmentIdRecord |
+------+------+--------------+----------------------+
| 2 | Tony | 1 | { id: 1, name: "dev"}|
+------+------+--------------+----------------------+
Basically having the departmentIdRecord column as a JSON containing the department record.
Is this even possible with MySql?
I have this table:
CREATE TABLE `test` (`id` INT(11) NOT NULL AUTO_INCREMENT , `name` VARCHAR(100) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB;
| id | name |
| -- | ---- |
In which I insert the following two rows:
INSERT INTO `test`(`name`) VALUES ('Oscar'), ('Alba')
| id | name |
| -- | ----- |
| 1 | Oscar |
| 2 | Alba |
Now, I want to update the existing rows and create new ones:
INSERT INTO `test` (`id`, `name`) VALUES (1, 'Oscar'), (2, 'Nadia'), ('', 'Pedro') ON DUPLICATE KEY UPDATE `id` = VALUES (`id`), `name` = VALUES (`name`)
| id | name |
| -- | ----- |
| 1 | Oscar |
| 2 | Nadia |
| 3 | Pedro |
If I update existing rows again and create new ones, this is what happens:
INSERT INTO `test` (`id`, `name`) VALUES (1, 'Oscar'), (2, 'Nadia'), (3, 'Lucas'), ('', 'Maria'), ('', 'Sergio') ON DUPLICATE KEY UPDATE `id` = VALUES (`id`), `name` = VALUES (`name`)
| id | name |
| -- | ------ |
| 1 | Oscar |
| 2 | Nadia |
| 3 | Lucas |
| 6 | Maria |
| 7 | Sergio |
There is a jump in the auto increment id. I don't know why this happens. I would like to know if there is any way to avoid this.
I have a table
table location_category {
id,
location_id,
category_id,
is_primary
}
What I want is to set a UNIQUE constraint for the combination location_id and is_primary.
I get that using this will make a multi column UNIQUE constraint
ALTER TABLE `votes` ADD UNIQUE `unique_index`(`location_id`, `is_primary`);
But my concern is that we can have multiple categories for a location but only set 1 category as primary.
For example:
| id | location_id | category_id | is_primary |
| 1 | 1 | 1 | 0 |
| 2 | 1 | 2 | 0 |
| 3 | 1 | 3 | 1 |
| 4 | 1 | 4 | 0 |
Will this violate the UNIQUE contraint? Since I have multiple instances of location_id = 1 and is_primary = 0?
Just trying to figure this out. Thank you for helping.
There is no need to change anything, UNIQUE allows multiple NULL values
CREATE TABLE `votes` (
`id` INTEGER,
`location_id` INTEGER,
`category_id` INTEGER,
`is_primary` INTEGER
);
ALTER TABLE `votes` ADD UNIQUE `unique_index`(`location_id`, `is_primary`);
INSERT INTO `votes`
(`id`, `location_id`, `category_id`, `is_primary`)
VALUES
('1', '1', '1', NULL),
('2', '1', '2', NULL),
('3', '1', '3', '1'),
('4', '1', '4', NULL);
SELECT * from `votes`
id | location_id | category_id | is_primary
-: | ----------: | ----------: | ---------:
1 | 1 | 1 | null
2 | 1 | 2 | null
3 | 1 | 3 | 1
4 | 1 | 4 | null
db<>fiddle here
So you can only have one location with is primary 1
Here are my tables.
Student Table
+++++++++++++++++++++++++++++++++++++++++++++++++++
| s_id | name | lname | fname | c_id |
+++++++++++++++++++++++++++++++++++++++++++++++++++
| 1 | ali | ahmadi | ahmad | 1 |
| 2 | Hussain | Sharan | Skekib | 2 |
| 3 | Mahmood | Shekibayee | Jahangir | 1 |
+++++++++++++++++++++++++++++++++++++++++++++++++++
Student_course Table
+++++++++++++++++++++++
| sc_id | s_id | c_id |
++++++++++++++++++++++
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 2 |
++++++++++++++++++++++
Now I want to have a trigger that:
When I insert new student into `student` table it should insert `s_id` and `c_id` into student_Course
table.
I am quite new to PHP and MySQL; any help will be highly appreciated.
Thanks in advance.
This could be fairly simple assuming that the sc_id is auto incremented primary key on Student_course
delimiter //
create trigger ins_student_course after insert on Student
for each row
begin
insert into Student_course (s_id,c_id) values (new.s_id,new.c_id);
end ;//
delimiter ;
Easiest way to explain is with an example:
mysql> select * from table_a left join table_b on col_a=col_b;
+-------+-------+
| col_a | col_b |
+-------+-------+
| 1 | NULL |
| 2 | NULL |
| 3 | 3 |
| 4 | 4 |
+-------+-------+
mysql> select * from table_a right join table_b on col_a=col_b;
+-------+-------+
| col_a | col_b |
+-------+-------+
| 3 | 3 |
| 4 | 4 |
| NULL | 5 |
| NULL | 6 |
+-------+-------+
But how do I get this?
mysql> select * from table_a ???? table_b on col_a=col_b;
+-------+-------+
| col_a | col_b |
+-------+-------+
| 1 | NULL |
| 2 | NULL |
| 3 | 3 |
| 4 | 4 |
| NULL | 5 |
| NULL | 6 |
+-------+-------+
Structure for #Abe:
CREATE TABLE IF NOT EXISTS `table_a` (
`col_a` int(11) NOT NULL,
PRIMARY KEY (`col_a`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `table_a` (`col_a`) VALUES
(1),
(2),
(3),
(4);
CREATE TABLE IF NOT EXISTS `table_b` (
`col_b` int(11) NOT NULL,
PRIMARY KEY (`col_b`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `table_b` (`col_b`) VALUES
(3),
(4),
(5),
(6);
Since there is no FULL OUTER JOIN option in MySQL I think you are limited to using a UNION:
select * from table_a left join table_b on col_a=col_b
UNION
select * from table_a right join table_b on col_a=col_b
This article provides several ways of simulating a full outer join with MySQL.
http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/
hope this helps.