While importing the sql data, it shows syntax error:
Here is my code:
CREATE TABLE IF NOT EXISTS `tblproduct` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
`image` text NOT NULL,
`price` double(10,2) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `product_code` (`code`)
)
INSERT INTO `tblproduct` (`id`, `name`, `code`, `image`, `price`) VALUES
(1, '3D Camera', '3DcAM01', 'product-images/camera.jpg', 1500.00),
(2, 'External Hard Drive', 'USB02', 'product-images/external-hard-drive.jpg', 800.00),
(3, 'Wrist Watch', 'wristWear03', 'product-images/watch.jpg', 300.00);
I just confused with this, what is the exact error.
Can anyone help me to fix this? thanks in advance.
Looks like it's whole generated so there should be no errors. Try to add semicolon after create table syntax so each query is separated. Like this:
CREATE TABLE IF NOT EXISTS `tblproduct` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`code` varchar(255) NOT NULL,
`image` text NOT NULL,
`price` double(10,2) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `product_code` (`code`)
);
INSERT INTO `tblproduct` (`id`, `name`, `code`, `image`, `price`) VALUES
(1, '3D Camera', '3DcAM01', 'product-images/camera.jpg', 1500.00),
(2, 'External Hard Drive', 'USB02', 'product-images/external-hard-drive.jpg', 800.00),
(3, 'Wrist Watch', 'wristWear03', 'product-images/watch.jpg', 300.00);
Related
I am trying to create and insert into 2 tables on sqlfiddle.
CREATE TABLE IF NOT EXISTS `Submissions` (
`sub_id` int(6) unsigned NOT NULL,
`hacker_id` int(3) unsigned NOT NULL,
`score` int(3) NOT NULL,
`sub_date` Date NOT NULL,
PRIMARY KEY (`sub_id`,`hacker_id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `Submissions` (`sub_id`, `hacker_id`, `score`, `sub_date`) VALUES
('18833', '962', '12', '2019-12-07'),
('35892', '962', '45', '2019-12-07');
CREATE TABLE IF NOT EXISTS `Hacker` (
`hacker_id` int(10) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY(`hacker_id`, `name`)
)
INSERT INTO `Hacker` (`hacker_id`, `name`) VALUES
('123','Bob');
The insert into Submissions works fine. I do not understand what is wrong with my Insert into Hacker which fails.
Fiddle is here: http://sqlfiddle.com/#!9/d4d786 but refreshing drops the second insert, I'm guessing because it fails to compile/build/whatever the term is in SQL.
You are missing a ; after your second CREATE TABLE statement. It should look like this:
CREATE TABLE IF NOT EXISTS `Submissions` (
`sub_id` int(6) unsigned NOT NULL,
`hacker_id` int(3) unsigned NOT NULL,
`score` int(3) NOT NULL,
`sub_date` Date NOT NULL,
PRIMARY KEY (`sub_id`,`hacker_id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `Submissions` (`sub_id`, `hacker_id`, `score`, `sub_date`) VALUES
('18833', '962', '12', '2019-12-07'),
('35892', '962', '45', '2019-12-07');
CREATE TABLE IF NOT EXISTS `Hacker` (
`hacker_id` int(10) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY(`hacker_id`, `name`)
);
INSERT INTO `Hacker` (`hacker_id`, `name`) VALUES
('123','Bob');
I have four tables I would like to join to the tables in the picture below
This is what i have come up with for now:
SELECT
category.category_id,
category.name,
menuitems.name AS menu_name,
restaurant.restaurant_id
FROM
category
JOIN
restaurant_has_category
ON
category.category_id = restaurant_has_category.category_id
AND
restaurant_has_category.category_id = category.category_id
JOIN
restaurant
JOIN
menuitems
The result is this:
As you can see I'm getting duplicates and the query thinks that the category name and menuItem name aren't distinct. I would love to show which categories a restaurant have AND then I would love to show all the different menuItems in each category
In the writing of this I realise that I need a separate query for all my menuItems ?
Can you see what im going for ? and my problem?
AS REQUESTED HERE IS THE CODE instead of pictures
CREATE TABLE IF NOT EXISTS `category` (
`category_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`description` MEDIUMTEXT,
`image` VARCHAR(255),
PRIMARY KEY (`category_id`)
);
CREATE TABLE IF NOT EXISTS `restaurant` (
`restaurant_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(150) NOT NULL,
`description` MEDIUMTEXT NOT NULL,
`image` VARCHAR(255),
`URL` VARCHAR(255),
`email` VARCHAR(100),
`openingHours` VARCHAR(255),
`CVR` INT(45),
`telephone` INT(45),
`place_id` INT(10) NOT NULL,
PRIMARY KEY (`restaurant_id`),
FOREIGN KEY (`place_id`) REFERENCES place(`place_id`)
);
CREATE TABLE IF NOT EXISTS `restaurant_has_category` (
`restaurant_id` INT(10) UNSIGNED NOT NULL,
`category_id` INT(10) NOT NULL,
PRIMARY KEY (`restaurant_id`, `category_id`),
FOREIGN KEY (`restaurant_id`) REFERENCES restaurant(`restaurant_id`),
FOREIGN KEY (`category_id`) REFERENCES category(`category_id`)
);
CREATE TABLE IF NOT EXISTS `menuItems` (
`menuItems_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL,
`toppings` VARCHAR(250),
`price` INT(10) NOT NULL,
`size` VARCHAR(50) NOT NULL,
`category_id` INT(10) NOT NULL,
PRIMARY KEY (`menuItems_id`),
FOREIGN KEY (`category_id`) REFERENCES category(`category_id`)
);
Here are som sample data to play around with;
INSERT INTO `menuitems` (`menuItems_id`, `name`, `toppings`, `price`,
`size`, `category_id`) VALUES
(1, 'Margherita', 'Tomatsovs, Ost', 55, '1pers', 1),
(4, 'Cheese Burger', 'Ost, salat, oksebøf', 89, '', 3),
(7, 'Durum rulle', 'Kebab, salat, tomat, agurk, skinke', 67, '', 2);
INSERT INTO `category` (`category_id`, `name`, `description`, `image`)
VALUES
(1, 'Pizza', 'beskrivelse', 'foto'),
(2, 'Ruller', 'beskrivelse', 'foto'),
(3, 'Burger', 'beskivelse', 'foto');
INSERT INTO `restaurant_has_category` (`restaurant_id`, `category_id`)
VALUES
(2, 1),
(2, 2),
(2, 3);
INSERT INTO `restaurant` (`restaurant_id`, `name`, `description`, `image`,
`URL`, `email`, `openingHours`, `CVR`, `telephone`, `place_id`) VALUES
(2, 'Alforno Pizza', 'Lorem ipsum dolor, description text here',
'https://migogkbh.dk/wp-content/uploads/2018/10/Kiin-Kiin-Bao-Bao-Foto-
Rasmus-Schou.jpg', 'hjemmeside', 'email#email.com', 'mo - th: 09:00', 0,
11223344, 1);
THIS IS THE FORMAT I'M SEEKING
category_id | category.name | menu_name | restaurant_id
1 Pizza Margherita 2
1 Pizza Kebab pizza 2
2 Burger Kebab burger 2
2 Burger Cheese burger 2
3 Durum kebab 2
You are getting duplication and misinformation because restaurant and menuitems are cross-joined to each other and the other tables. Fix your FROM clause and you will get the data you expect.
You can find basic SQL tutorials all over the web. My favorite beginner SQL training is at W3CSchools.com. But don't stop there. You should also try to understand set relationships and how those are defined in terms of SQL code. I find presentations that use Venn diagrams can be helpful.
I have a database of the following relations:
Bar(Name, Address, Licence)
Beer(Name,Manufacture)
Drinker(Name,Address)
Frequents(DrinkerName,BarName)
Likes(DrinkerName,BeerName)
Sells(BarName,BeerName,Amount)
Serves(BarName,BeerName)
The Sample DDL Statements:
CREATE TABLE `Bar` (
`Name` varchar(255) NOT NULL,
`Address` varchar(255) DEFAULT NULL,
`Licence` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Bar` (`Name`, `Address`, `Licence`) VALUES
('Deluxe', 'Luxvagen 2', 'Yes'),
('Grace', 'Gracevagen 2', 'Yes'),
('KrogBar', 'Barvagen 2', 'Yes');
CREATE TABLE `Beer` (
`Name` varchar(255) NOT NULL,
`Manufacture` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Beer` (`Name`, `Manufacture`) VALUES
('Carlsberg', 'Coppers'),
('Heiniken', 'Spendrups'),
('Miller', 'DaMill');
CREATE TABLE `Boor` (
`Name` varchar(255) NOT NULL,
`Age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Drinker` (
`Name` varchar(255) NOT NULL,
`Address` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Drinker` (`Name`, `Address`) VALUES
('Alex', 'Överbar 2'),
('Bam', 'Påbar 2'),
('Emil', 'Mittibar 2'),
('Max', 'Ibar 2'),
('Petra', 'Förebar 2'),
('Rebecca', 'Efterbar 2'),
('Sam', 'Underbar 2');
CREATE TABLE `Frequents` (
`DrinkerName` varchar(255) NOT NULL DEFAULT '',
`BarName` varchar(255) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Frequents` (`DrinkerName`, `BarName`) VALUES
('Emil', 'Deluxe'),
('Max', 'Deluxe'),
('Rebecca', 'Deluxe'),
('Alex', 'Grace'),
('Petra', 'Grace'),
('Bam', 'KrogBar'),
('Sam', 'KrogBar');
CREATE TABLE `Likes` (
`DrinkerName` varchar(255) NOT NULL DEFAULT '',
`BeerName` varchar(255) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Likes` (`DrinkerName`, `BeerName`) VALUES
('Bam', 'Carlsberg'),
('Emil', 'Carlsberg'),
('Rebecca', 'Carlsberg'),
('Emil', 'Heiniken'),
('Max', 'Heiniken'),
('Petra', 'Heiniken'),
('Sam', 'Heiniken'),
('Alex', 'Miller');
CREATE TABLE `Sells` (
`BarName` varchar(100) DEFAULT NULL,
`BeerName` varchar(100) DEFAULT NULL,
`Amount` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Sells` (`BarName`, `BeerName`, `Amount`) VALUES
('KrogBar', 'Miller', 3),
('KrogBar', 'Carlsberg', 2),
('KrogBar', 'Heiniken', 1),
('Deluxe', 'Heiniken', 1);
CREATE TABLE `Serves` (
`BarName` varchar(255) NOT NULL DEFAULT '',
`BeerName` varchar(255) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Serves` (`BarName`, `BeerName`) VALUES
('Grace', 'Carlsberg'),
('KrogBar', 'Carlsberg'),
('Deluxe', 'Heiniken'),
('Grace', 'Heiniken'),
('KrogBar', 'Heiniken'),
('KrogBar', 'Miller');
I want to find drinkers that frequent only bars that serve beer that they like (the assumption is that each drinker frequents at least one bar).
How can I construct such a query? I know that I have to use Joins and Sub-queries, I am not new to either of them but all my implementations have not yielded the correct results.
Is this what you looking for;
Select DISTINCT bar.Name,bar.Address,frequents.DrinkerName, likes.BeerName
From
Bar
Join frequents on bar.Name = frequents.BarName
join sells on bar.name = sells.BarName
join likes on frequents.DrinkerName = likes.DrinkerName
I'm trying to do a full-text search in two separate tables and sort the results by relevancy. I am not getting a relevant data. below is my tables structure and my query which I am using
- I have two tables "deals" and "outlets" now I want to do full text search as user can search with title only and with the address as well. Please see the tables and query
Table1:
CREATE TABLE `deals` (
`id` int(11) NOT NULL,
`outlet_store_id` int(6) DEFAULT NULL,
`title` varchar(250) DEFAULT NULL,
`description` text,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `deals` (`id`, `outlet_store_id`, `title`, `description`, `created_at`, `updated_at`) VALUES
(1, 1, 'Great Deal Flat 50% Off', '40% of on all the shoes', '2017-09-10 00:00:00', '2017-09-05 05:08:14'),
(5, 4, 'Flat 50% off', 'sdfsdf sfsdf ds', '2017-09-12 11:54:14', '2017-09-12 11:57:03');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `deals`
--
ALTER TABLE `deals`
ADD PRIMARY KEY (`id`);
ALTER TABLE `deals` ADD FULLTEXT KEY `title` (`title`);
**Table 2**
CREATE TABLE `outlet_stores` (
`id` int(11) NOT NULL,
`name` varchar(200) DEFAULT NULL,
`address` text,
`longitude` varchar(30) DEFAULT NULL,
`latitude` varchar(30) DEFAULT NULL,
`status` tinyint(2) NOT NULL DEFAULT '1',
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `outlet_stores`
--
INSERT INTO `outlet_stores` (`id`, `name`, `address`, `longitude`, `latitude`, `status`, `created_at`, `updated_at`) VALUES
(1, 'Polo-011', 'SCO-89,90,91, 1st Floor, Sector 17 D', '76.755181', '30.734015', 0, '2017-08-22 00:00:00', '2017-09-06 11:47:49'),
(3, 'Tommy Highflier', 'SCO 11-12, Sector 17D, Chandigarh', NULL, NULL, 1, '2017-09-05 11:09:48', '2017-09-05 11:09:48'),
(4, 'Kapson Store', 'Elante Mall, Chandigarh', NULL, NULL, 1, '2017-09-12 11:20:50', '2017-09-12 11:20:50');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `outlet_stores`
--
ALTER TABLE `outlet_stores`
ADD PRIMARY KEY (`id`);
ALTER TABLE `outlet_stores` ADD FULLTEXT KEY `address` (`address`);
What I want
There is only one input on my website and User can search with "Flat 50% off" and "Flat 50% off in elante mall" so after my query I am getting same records I want if user search with "flat" then it returns all the records and if user search with location then it return actual match and if there is no records then it returns empty but some suggestions suppose "Flat 50% off in elante mall" if there is no record then it search with flat 50% and returns some suggestion results.
My query which I am using
SELECT *,
MATCH(deals.title) AGAINST($q) as deals,
MATCH(outlet_stores.address) AGAINST($q) as outlet_stores
FROM deals
LEFT JOIN outlet_stores ON deals.outlet_store_id = outlet_stores.id
WHERE
MATCH(deals.title) AGAINST($q)
OR MATCH(outlet_stores.address) AGAINST($q);
I need to be able to determine all of the people that were missing from attendance in a series of meetings.
I have a solution to figure this problem out with JS on the client's computer but I think it could be done more efficiently on the server.
Table A (people) -> Table B (attendance) <- Table C(meeting)
The attendance is a mapping table of items in table A and C.
See: http://sqlfiddle.com/#!8/6db81 for the exact schema
What I want is to determine all of the meetings that people have missed. That is there is no entry for that person for that meeting in the attendance table B.
Desired output should include a minimum of the lid (user id) and mid (meeting ID).
lid, firstname, lastname, mid, meeting_title, start.
The solution in JS would be to send the results of a cross of A and C, and the results of B to the client. Then remove all of the items in B from the cross of A and C.
CREATE TABLE IF NOT EXISTS `attendance` (
`mid` bigint(20) NOT NULL,
`sid` bigint(20) DEFAULT NULL,
`entered` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`lid` varchar(64) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`mid`,`lid`),
KEY `entered` (`entered`)
);
INSERT INTO `attendance` (`mid`, `sid`, `entered`, `lid`) VALUES
(5, NULL, '2013-12-25 21:44:27', '100'),
(5, NULL, '2013-12-25 21:44:19', '200'),
(5, NULL, '2013-12-25 21:44:21', '300'),
(9, NULL, '2013-12-26 14:49:49', '200'),
(9, NULL, '2013-12-26 07:10:34', '300');
CREATE TABLE IF NOT EXISTS `meetings` (
`mid` bigint(11) NOT NULL AUTO_INCREMENT,
`title` varchar(32) CHARACTER SET utf8 NOT NULL,
`start` datetime NOT NULL COMMENT 'registration start time',
`stop` datetime NOT NULL COMMENT 'registration stop time',
PRIMARY KEY (`mid`),
UNIQUE KEY `title` (`title`)
);
INSERT INTO `meetings` (`mid`, `title`, `start`, `stop`) VALUES
(5, 'Meeting 1', '2013-12-25 01:12:00', '2013-12-25 23:12:00'),
(9, 'Meeting 2', '2013-12-26 01:00:00', '2013-12-26 23:00:00');
CREATE TABLE IF NOT EXISTS `people` (
`sid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`lid` varchar(64) NOT NULL,
`firstname` varchar(64) NOT NULL,
`lastname` varchar(64) NOT NULL,
`title` varchar(5) DEFAULT NULL,
`address` varchar(128) DEFAULT NULL,
`city` varchar(64) DEFAULT NULL,
`state` varchar(2) DEFAULT NULL,
`zip` varchar(9) DEFAULT NULL,
`phone` varchar(12) DEFAULT NULL,
`cell` varchar(12) DEFAULT NULL,
`email` varchar(64) DEFAULT NULL,
PRIMARY KEY (`sid`),
UNIQUE KEY `sid` (`sid`),
UNIQUE KEY `lid` (`lid`)
);
INSERT INTO `people` (`sid`, `lid`, `firstname`, `lastname`, `title`, `address`, `city`, `state`, `zip`, `phone`, `cell`, `email`) VALUES
(1, '100', 'Fred', 'Jones', 'Mr.', 'Somewhere', 'City', 'AK', '12345', '123-123-1234', '123-123-1234', 'email#email.com'),
(2, '200', 'Wilma', 'Jones', 'Mrs.', '', 'City', '', '12346', '', NULL, '');
You have to join people and meetings table to get all possible combinations of meeting id and userid and then filter out only those, which are not present in attendance table.
SELECT a.lid,
b.mid
FROM people a
CROSS JOIN meetings b
WHERE NOT EXISTS (SELECT 1
FROM attendance c
WHERE c.mid = b.mid
AND c.lid = a.lid);
Fiddle