I have created the simple table in a MySQL database;
CREATE TABLE `opportunity` (
`customer` varchar(255) NOT NULL DEFAULT '',
`category` varchar(255) NOT NULL DEFAULT '',
`bill_amount` decimal(13,2) NOT NULL,
PRIMARY KEY (`bill_amount`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I used 'bill_amount' as my primary key so I could remove duplicate information when I import data to the table, but I need to also need to separate records by separate categories as well. Here is an example of what I have, what results I am getting, and what I need. If anyone could give me advice on how to properly set up my table so it groups the data as need I would appreciate the help.
Related
There's a table I have now:
I can't think of a query that would make the order of 'name' column that i need.
I need it to go like this:
ads->work_and_business->search_work->require_work->freelance->transport->cars->trucks->water->air->...
so, basically i need like parent->all children, next parent->all children
can someone help with this?
Edit: table create script:
CREATE TABLE categories (
id int(11) NOT NULL AUTO_INCREMENT,
level int(11) NOT NULL DEFAULT '1',
parent_category_id int(11) NOT NULL DEFAULT '1',
name varchar(255) DEFAULT NULL,
PRIMARY KEY (id),
KEY FK_categories_categories (parent_category_id)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
Edit2: level is needed for something else in php part. in query part it's not needed
I tried with your sample data and i think this works
SELECT distinct(ifnull(c1.name,c.name)) as name
from categories c
right JOIN categories c1 ON c.id=c1.parent_category_id
order by c.parent_category_id;
I have two MySQL Tables
CREATE TABLE IF NOT EXISTS `orders` (
`order_id` int(5) NOT NULL AUTO_INCREMENT,
`order_address` varchar(255) NOT NULL,
`order_paymentmethod` varchar(50) NOT NULL,
`coupon_code` varchar(50) NOT NULL,
`user_id` int(5) NOT NULL,
PRIMARY KEY (`order_id`),
KEY `fk_orderuser` (`user_id`),
KEY `fk_ordercoupon` (`coupon_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `coupons` (
`coupon_code` varchar(50) NOT NULL,
`coupon_discount` int(255) NOT NULL,
PRIMARY KEY (`coupon_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
When am deleting the coupon code from the coupon table the order record that the coupon is related too is also deleted. And i just want to delete the coupon code without any effect on the table orders
Is their any solution for that please?
Regards
In this case you can solve that problem by using mysql trigger. Create trigger for coupons table
CREATE TRIGGER `coupons_before_delete` AFTER DELETE ON `coupons` FOR EACH ROW BEGIN
-- INSERT INTO add_inout_to_error_log(msg) VALUES(old.coupon_code);
DELETE FROM orders WHERE orders.coupon_code = old.coupon_code;
END
in this code old.coupon_code is current deleted coupon code. You can get access to any field of deleted item
There are three options here
Don't delete the coupon, use another boolean field (eg deleted) with default = true, but set it to false when you want to remove it (it doesn't actually remove it but you can handle deleted coupons using this field). This way you can also track the orders initiated with a coupon that on the way this was deleted.
Remove the not null constraint from coupon_code varchar(50) NOT NULL (in orders table) and add a foreign key constraint to ON DELETE SET NULL. *For orders without a coupon set it to null from the beginning.
Using the known as NULL pattern. Create a dummy coupon (ZERO discount) in your db and instead of deleting coupons, assign this dummy coupon to orders that do not require a real coupon.
*depending on the "tracking" requirements, a combination of the above approaches may be required
I create the MySql table by the following sql statement:
CREATE TABLE IF NOT EXISTS `mytable` (
`agent` varchar(64) NOT NULL,
`name` varchar(40) NOT NULL,
`app` varchar(64) NOT NULL,
PRIMARY KEY (`app`,`agent`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
As you see, the field 'app' and 'agent' is the primary key. But unfortunately it doesn't work when I insert the following data, it always show the duplicated key in 'app' field:
app agent name
-------------------------
MyApp ios cde
MyApp android abc
Can anybody tell me anything wrong? Thanks
In your primary key app and agent are a primary key together, not two individual keys.
You'll be able to add many rows with app = 'MyApp' as long as agent differs. And the other way around.
If you wan't to disallow multiple rows with the same app and multiple rows with the same agent add normal unique indexes.
CREATE TABLE IF NOT EXISTS `mytable` (
`agent` varchar(64) NOT NULL,
`name` varchar(40) NOT NULL,
`app` varchar(64) NOT NULL,
UNIQUE app_index (`app`),
UNIQUE agent_index (`agent`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
The set of primary keys in MySQL does not check for individual unique values, it will give duplicate error when you will try to insert same set of values in multiple records, but both the columns will not accept NULL values
Eg.
app agent name
-------------------------
MyApp ios cde
MyApp ios abc - it will give you error as "Duplicate entry 'MyApp-ios' for key 'PRIMARY'"
may this will help you
I'm looking to append a comments table from one WordPress site to another. The users are different. When I import the comments from site B to A, I run into a duplicate key issue; comment_id is already taken.
So how can I resolve this and append the table with a simple .sql file? Would I have to take the user information, generate a new user, check for comments made on site B, pull the content and postID, then go back to site A and recreate the comment for the newly created user!?
What a headache! THanks.
if your only problem is a duplicate key issue, go to the end of your sql file after
ENGINE=MyISAM
and make it
ENGINE=MyISAM AutoIncrement=a nubmer above the last id in the new database
or
Query database A for the last id then add one and use it on a new insert query.
Example 1:
CREATE TABLE IF NOT EXISTS `movies` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`year` int(4) NOT NULL,
`size` varchar(255) NOT NULL,
`added` date NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`,`year`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
The Inserts From My Dump:
INSERT INTO `movies` (`title`, `year`, `size`, `added`) VALUES
('[REC] 2', 0, '716688', '2011-09-23'),
('5 Days of War', 0, '1435406', '2012-01-09'),
('[REC]', 0, '1353420800', '2011-11-06');
See how i didnt include the PRIMARY KEY (id) in my includes, but it will still check against my UNIQUE KEY and see if the title exists. Just a little demo that hopefully helps out. If your table already exists on the new database then just skip to the inserts and dont include the primary key and it will be auto set on a new insert to the next available value.
So I've got a table with all users, and their values. And I want to order them after how much "money" they got. The problem is that they have money in two seperate fields: users.money and users.bank.
So this is my table structure:
CREATE TABLE IF NOT EXISTS `users` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(54) COLLATE utf8_swedish_ci NOT NULL,
`money` bigint(54) NOT NULL DEFAULT '10000',
`bank` bigint(54) NOT NULL DEFAULT '10000',
PRIMARY KEY (`id`),
KEY `users_all_money` (`money`,`bank`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci AUTO_INCREMENT=100 ;
And this is the query:
SELECT id, (money+bank) AS total FROM users FORCE INDEX (users_all_money) ORDER BY total DESC
Which works fine, but when I run EXPLAIN it shows "Using filesort", and I'm wondering if there is any way to optimize it?
Because you want to sort by a derived value (one that must be calculated for each row) MySQL can't use the index to help with the ordering.
The only solution that I can see would be to create an additional total_money or similar column and as you update money or bank update that value too. You could do this in your application code or it would be possible to do this in MySQL with triggers too if you wanted.