MySql join statement returning too many results - mysql

I am attempting to join some columns from two tables like follows:
select routes.route_collection, times.times
from routes
inner join times
on routes.bus_id & times.bus_id =1;
Although I am getting 12 results (duplicated by 4) instead of three.
Am I doing something wrong here? I have looked at several sites and they all appear to work using similar syntax.
Edit: It appears to be multiplying the amount of values in one table by another - in this case 4 x 3?
Here is the database schema
-- --------------------------------------------------------
-- Host: 127.0.0.1
-- Server version: 5.5.32 - MySQL Community Server (GPL)
-- Server OS: Win32
-- HeidiSQL Version: 8.0.0.4459
-- --------------------------------------------------------
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
-- Dumping database structure for mydb
CREATE DATABASE IF NOT EXISTS `mydb` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `mydb`;
-- Dumping structure for table mydb.buses
CREATE TABLE IF NOT EXISTS `buses` (
`bus_id` int(11) NOT NULL,
PRIMARY KEY (`bus_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Dumping data for table mydb.buses: ~5 rows (approximately)
/*!40000 ALTER TABLE `buses` DISABLE KEYS */;
INSERT INTO `buses` (`bus_id`) VALUES
(1),
(2),
(3),
(4),
(5);
/*!40000 ALTER TABLE `buses` ENABLE KEYS */;
-- Dumping structure for table mydb.routes
CREATE TABLE IF NOT EXISTS `routes` (
`route_id` int(11) NOT NULL,
`bus_id` int(11) DEFAULT NULL,
`route_collection` varchar(45) DEFAULT NULL,
`stop_order` int(11) DEFAULT NULL,
PRIMARY KEY (`route_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Dumping data for table mydb.routes: ~4 rows (approximately)
/*!40000 ALTER TABLE `routes` DISABLE KEYS */;
INSERT INTO `routes` (`route_id`, `bus_id`, `route_collection`, `stop_order`) VALUES
(1, 1, 'Douglas,Onchan,Ballasalla,Castletown,Colby', 1),
(2, 1, 'Douglas,Onchan,Ballasalla,Castletown,Colby', 2),
(3, 1, 'Douglas,Onchan,Ballasalla,Castletown,Colby', 3),
(4, 1, 'Douglas,Onchan,Ballasalla,Castletown,Colby', 4);
/*!40000 ALTER TABLE `routes` ENABLE KEYS */;
-- Dumping structure for table mydb.times
CREATE TABLE IF NOT EXISTS `times` (
`time_id` int(11) NOT NULL,
`start_time` int(11) DEFAULT NULL,
`bus_id` int(11) DEFAULT NULL,
`times` varchar(500) DEFAULT NULL,
`period` varchar(45) DEFAULT NULL,
PRIMARY KEY (`time_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Dumping data for table mydb.times: ~3 rows (approximately)
/*!40000 ALTER TABLE `times` DISABLE KEYS */;
INSERT INTO `times` (`time_id`, `start_time`, `bus_id`, `times`, `period`) VALUES
(1, 600, 1, '06:00,07:00,12:00,14:00,23:00', 'MonFri'),
(2, 615, 1, '06:15,07:15,12:15,14:15,23:15', 'MonFri'),
(3, 600, 1, '06:00,07:00,12:00,14:00,23:00', 'Sat');
/*!40000 ALTER TABLE `times` ENABLE KEYS */;
/*!40101 SET SQL_MODE=IFNULL(#OLD_SQL_MODE, '') */;
/*!40014 SET FOREIGN_KEY_CHECKS=IF(#OLD_FOREIGN_KEY_CHECKS IS NULL, 1, #OLD_FOREIGN_KEY_CHECKS) */;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;

Please use operator 'AND' instead of '&'.
And as I understand you need something like that (do not know do you need the where clouse):
select routes.route_collection, times.times
from routes
inner join times
on routes.bus_id = times.bus_id
where routes.bus_id = 1;
May be you need aggregated values of times?
something like this:
select routes.route_collection, GROUP_CONCAT(times.times)
from routes
inner join times
on routes.bus_id = times.bus_id
where routes.bus_id = 1
group by routes.route_id;

Related

One column in a table referring to another column where there are duplicate data in SQL

I want to build simple database system about bike (or bicycle) rental store.
Here are the tables, and columns in parantheses:
Model (MNr, Modelname, dayprice)
Bike (MNr, CopyNr, Frame, Color)
Customer (CNr, Name, Surename, MobileNr)
Rental (CNr, MNr, CopyNr, DateOut, DateIn)
(MNr stands for model number, CNr stands for customer number).
Models are bike models, Bike table shows data on each particular bike.
There can be several bikes of each model, to distinguish between them
"CopyNr" column is used, where each bike in a given model is numbered 1, 2, 3, etc.
Rental: A rental relationship always applies to only one bike and one customer.
So I put the following primary and foreign keys:
Model (MNr (PK), Modelname, dayprice)
Bike (MNr (FK), CopyNr (PK), Frame, Color)
Customer (CNr (PK), Firstame, Surname, MobileNr)
Rent (CNr (FK), MNr (FK), CopyNr (FK), DateOut, DateIn)
The "CopyNr" at "Rental" is referring to the "CopyNr" of "Bikes". So I used "CopyNr" as
primary key column.
But that column can contain duplicate values, like there can be copy nr 2 of model A and copy nr 2 of
model B.
And primary key columns don't allow duplicate values.
I wrote the following in the SQL file, and when but it on PHPMyAdmin.
Here's the code from SQL code:
[It had Norwegian names, I changed it to English, thus color names are Norwegian]
-- phpMyAdmin SQL Dump
-- version 4.8.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Feb 04, 2020 at 07:15 PM
-- Server version: 10.2.26-MariaDB-log
-- PHP Version: 7.1.30
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+01:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
--
--
-- --------------------------------------------------------
--
-- Table structure for table `Customer`
--
CREATE TABLE `Customer` (
`CNr` int(2) NOT NULL,
`Firstname` varchar(20),
`Surname` varchar(20),
`MobileNr` varchar(8),
PRIMARY KEY (CNr)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Customer` (`CNr`, `Firstname`, `Surname`, `MobileNr`) VALUES
(1, 'Olav', 'Petterses', '88888888'),
(2, 'Petter', 'Olavsson', '44444444');
-- --------------------------------------------------------
--
-- Table structure for table `Model`
--
CREATE TABLE `Model` (
`MNr` int(5) NOT NULL,
`Modelname` varchar(20),
`Dayprice` double DEFAULT NULL,
PRIMARY KEY (MNr)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Model` (`MNr`, `Modelname`, `Dayprice`) VALUES
(2, 'FirstPrice', 10),
(15, 'DBS', 50),
(16, 'DBS', 60);
-- --------------------------------------------------------
--
-- Table structure for table `Bike`
--
CREATE TABLE `Bike` (
`MNr` int(5),
`CopyNr` int(2) NOT NULL,
`Frame` int(3),
`Color` varchar(10),
PRIMARY KEY (`CopyNr`, `MNr`),
CONSTRAINT `Bike_ibfk_1` FOREIGN KEY (`MNr`) REFERENCES `Model` (`MNr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Bike` (`MNr`, `CopyNr`, `Frame`, `Color`) VALUES
(2, 1, 55, 'rød'),
(15, 1, 65, 'rød'),
(16, 1, 55, 'grønn'),
(2, 2, 55, 'rød'),
(15, 2, 65, 'grønn');
-- --------------------------------------------------------
--
-- Table structure for table `Rent`
--
CREATE TABLE `Rent` (
`CNr` int(2),
`MNr` int(5),
`CopyNr` int(2),
`DatoUt` date,
`DateInn` date,
CHECK (`DatoUt` < `DateInn`),
CONSTRAINT `Rent_ibfk_1` FOREIGN KEY (`MNr`) REFERENCES `Model` (`MNr`),
CONSTRAINT `Rent_ibfk_2` FOREIGN KEY (`CopyNr`) REFERENCES `Bike` (`CopyNr`),
CONSTRAINT `Rent_ibfk_3` FOREIGN KEY (`CNr`) REFERENCES `Customer` (`CNr`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `Rent` (`CNr`, `MNr`, `CopyNr`, `DatoUt`, `DateInn`) VALUES
(1, 15, 1, '2020-01-01', '2020-01-30'),
(2, 15, 2, '2020-02-15', '2020-02-29');
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
When I put it on SQL on PHPAdmin and click "GO", I got error because of CHECK ( DatoIn > DateUt) code.
And I remove that part, then I get "Rent" table empty of values.
What's wrong am I doing?
Why is the "Rent" table empty of values when I put this on SQL on PHPMyAdmin?
Should I restructure the table relations?
And why "CHECK" doesn't work?
The error message I get where I use CHECK:
I believe you're seeing the syntax warning due to a bug in the parser used by phpMyAdmin which improperly marks this syntax as incorrect. I think that bug specifically is already reported as https://github.com/phpmyadmin/sql-parser/issues/167.
As nick indicates, you can still use the query despite the syntax warning.
I think that the date out should've be less than the date in
CHECK (DatoUt < DateInn),

Data Is not getting inserted in Mysql Database in a Node.js Real time notification app

I am trying to build a notification app which can notify changes in msql database using Nodejs and socket.io .
But mydata is not getting inserted in database. Attaching my db.js file and socketDemo.sql. My database name is socket.io
db.js file:-
var addComment = function(user,comment,mysql,pool,callback) {
console.log(user,comment);
var self = this;
pool.getConnection(function(err,connection){
if (err) {
//connection.release();
return callback(true,null);
} else {
var sqlQuery = "INSERT into UserComment (UserName,UserId,Comment) VALUES ((SELECT UserName FROM User WHERE UserName = user),id,comment)";
// var inserts =["UserComment","UserId","UserName",
// "Comment","UserId","User","UserName",
// user,user,comment];
//sqlQuery = mysql.format(sqlQuery,inserts);
connection.query(sqlQuery,function(err,rows){
connection.release();
if (err) {
return callback(true,null);
} else {
callback(false,"comment added");
}
});
}
connection.on('error', function(err) {
return callback(true,null);
});
});
};
module.exports.addComment = addComment;
socketDemo.sql:-
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `socketDemo`
--
-- --------------------------------------------------------
--
-- Table structure for table `User`
--
CREATE TABLE IF NOT EXISTS `User` (
`UserId` int(11) NOT NULL,
`UserName` varchar(25) NOT NULL,
`Password` varchar(25) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `User`
--
INSERT INTO `User` (`UserId`, `UserName`, `Password`) VALUES
(1, 'Harshit', 'Harshit');
-- --------------------------------------------------------
--
-- Table structure for table `UserComment`
--
CREATE TABLE IF NOT EXISTS `UserComment` (
`UserId` int(11) NOT NULL,
`UserName` varchar(11) NOT NULL,
`Comment` text NOT NULL,
`PostId` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `UserComment`
--
INSERT INTO `UserComment` (`UserId`, `UserName`, `Comment`, `PostId`) VALUES
(1, 'Harshit', '\n \n \n \n \n ', 0);
-- --------------------------------------------------------
--
-- Table structure for table `UserPost`
--
CREATE TABLE IF NOT EXISTS `UserPost` (
`UserPostId` int(11) NOT NULL,
`UserPostContent` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `UserPost`
--
INSERT INTO `UserPost` (`UserPostId`, `UserPostContent`) VALUES
(1, 'This is test comment.');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `User`
--
ALTER TABLE `User`
ADD PRIMARY KEY (`UserName`),
ADD KEY `UserIdIndex` (`UserId`);
--
-- Indexes for table `UserComment`
--
ALTER TABLE `UserComment`
ADD KEY `UserIdIndexComment` (`UserId`),
ADD KEY `PostIdIndex` (`PostId`);
--
-- Indexes for table `UserPost`
--
ALTER TABLE `UserPost`
ADD PRIMARY KEY (`UserPostId`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `User`
--
ALTER TABLE `User`
MODIFY `UserId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
--
-- AUTO_INCREMENT for table `UserPost`
--
ALTER TABLE `UserPost`
MODIFY `UserPostId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=#OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=#OLD_COLLATION_CONNECTION */;
Database schema:-
enter image description here
please help.
My first question is why do you want to use SELECT UserName FROM User WHERE UserName = user - you can simply use the user variable instead the SELECT statement.
What is more, if you are performing such a query and you want to use variables passed to the function, you need to pass them to the .query method:
connection.query("INSERT into UserComment (UserName,UserId,Comment) VALUES (?, ?, ?)", [user, id, comment], function(error, results){
// check error and perform further operations...
});
The [user, id, comment] part is used to replace the ? marks in the SQL query (remember to maintain order of those variables in the array).

Can I use select statements when inserting into a table?

I have the following table:
DROP TABLE IF EXISTS `worklist`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `worklist` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`ENTITY_TYPE_CODE` bigint(20) NOT NULL,
`TYPE_CODE` bigint(20) NOT NUll,
`NAME` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=125 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
Insert INTO code (DISPLAY, CODE_GROUP, CODE_KEY, ALIAS) VALUES ('Duplicate Claims Worklist', 'WORKLIST_TYPE', 'DUPLICATE_CLAIM_WORKLIST', 'Duplicate Claims Worklist');
INSERT INTO code (DISPLAY, CODE_GROUP, CODE_KEY, ALIAS) VALUES ('Claim', 'ENTITY', 'CLAIM', 'Claim');
INSERT INTO worklist (ENTITY_TYPE_CODE, TYPE_CODE) VALUES (SELECT ID FROM code WHERE CODE_GROUP = 'ENTITY' and CODE_KEY = 'CLAIM', SELECT ID from code where CODE_GROUP = 'WORKLIST_TYPE' and CODE_KEY = 'DUPLICATE_CLAIM_WORKLIST');
ALTER TABLE workitems ADD WORKITEMS_ID bigint(20) DEFAULT NULL;
Yes you can you need to omit the Values keyword and do something like this
INSERT INTO code (DISPLAY, CODE_GROUP, CODE_KEY, ALIAS)
SELECT DISPLAY, CODE_GROUP, CODE_KEY, ALIAS
FROM SomeTable
Obviously you will need to include a where clause to control the select statement but that's basically it.
EDIT:
It would look something like this (code is untested and has been typed directly into the editor so may have some speeling mistooks.)
DROP TABLE IF EXISTS `worklist`;
/*!40101 SET #saved_cs_client = ##character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `worklist` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`ENTITY_TYPE_CODE` bigint(20) NOT NULL,
`TYPE_CODE` bigint(20) NOT NUll,
`NAME` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=125 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = #saved_cs_client */;
Insert INTO code (DISPLAY, CODE_GROUP, CODE_KEY, ALIAS) VALUES ('Duplicate Claims Worklist', 'WORKLIST_TYPE', 'DUPLICATE_CLAIM_WORKLIST', 'Duplicate Claims Worklist');
INSERT INTO code (DISPLAY, CODE_GROUP, CODE_KEY, ALIAS) VALUES ('Claim', 'ENTITY', 'CLAIM', 'Claim');
INSERT INTO worklist (ENTITY_TYPE_CODE, TYPE_CODE) SELECT A.ID, B.ID FROM (SELECT ID, 1 AS J FROM code WHERE CODE_GROUP = 'ENTITY' and CODE_KEY = 'CLAIM') A LEFT JOIN ( SELECT ID, 1 AS J FROM code WHERE CODE_GROUP = 'WORKLIST_TYPE' and CODE_KEY = 'DUPLICATE_CLAIM_WORKLIST') B ON A.J = B.J;
ALTER TABLE workitems ADD WORKITEMS_ID bigint(20) DEFAULT NULL;

Error code:1054.Unknown column 'user_id'in 'field list'

I am having a problem. When I run this code in MySql Workbench 6.3.3, i have this error :
Error code:1054.Unknown column 'user_id'in 'field list'.
What should I do?
use Proiecte;
CREATE TABLE users (i INT) ENGINE=MYISAM;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
REPLACE INTO `users` (`user_id`, `first_name`, `last_name`, `gender`, `city`) VALUES
(906, 'Ankush', 'Thakur', 'male', 'gurgaon'),
(907, 'Anamika', 'Singh', 'female', 'meerut'),
(908, 'Shweta', 'Gupta', 'female', 'gurgaon'),
(909, 'Rajesh', 'Chauhan', 'male', 'noida'),
(911, 'Andrew', 'Symonds', 'male', 'delhi');
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
/*!40014 SET FOREIGN_KEY_CHECKS=1 */;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
DESC users
the problem in create table command:
CREATE TABLE users (i INT) ENGINE=MYISAM;
you are creating table with only 1 column and then trying to insert the data in 5 columns..
create table with required 5 columns..
CREATE TABLE users (`user_id` int(10), `first_name` varchar(100), `last_name` varchar(100), `gender` varchar(10), `city` varchar(100)) ENGINE=MYISAM;

What would be the most efficient way of creating my MySQL database

I'm thinking of doing something like...
Table Pants (20 entrees)
| ID | Item | Description | Price
Table Shirts (20 entrees)
| ID | Item | Description | Price
Table Socks (5 entrees)
| ID | Item | Description | Price
Then I run a php foreach ID in $table, to populate the list on a page.
So would this be an efficient way of doing this since some of my tables might not have many entrees? Or should I have all the entrees in one table and add an extra field for the different categories and find another way of populating my page via the new field? Or maybe a greater idea?
Thanks for your time.
your model is not flexible and not efficient. Your second solution is the good one create a table named items and add an extra field named categories which should be a foreign key.
you can check this http://sqlfiddle.com/#!2/74c91/1, here is all you need
here is the code needed for the database creation :
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET FOREIGN_KEY_CHECKS=0 */;
-- Dumping structure for table database.categories
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table database.categories: ~0 rows (approximately)
/*!40000 ALTER TABLE `categories` DISABLE KEYS */;
INSERT INTO `categories` (`id`, `name`, `created`) VALUES
(1, 'Pants ', '2012-12-15 11:37:27'),
(2, 'Shirts ', '2012-12-15 11:37:36'),
(3, 'Socks ', '2012-12-15 10:38:47');
/*!40000 ALTER TABLE `categories` ENABLE KEYS */;
-- Dumping structure for table database.clothes
CREATE TABLE IF NOT EXISTS `clothes` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`item` varchar(255) NOT NULL,
`description` text,
`category_id` int(10) NOT NULL,
`created` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `category_id` (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Dumping data for table database.clothes: ~0 rows (approximately)
/*!40000 ALTER TABLE `clothes` DISABLE KEYS */;
INSERT INTO `clothes` (`id`, `item`, `description`, `category_id`, `created`) VALUES
(1, 'red socks', 'Red Socks Rocks', 3, '2012-12-15 10:39:06'),
(2, 'blue socks', 'nice socks too', 3, '2012-12-15 10:39:18'),
(3, 'grey pants', 'pretty cool', 1, '2012-12-15 10:40:34'),
(4, 'Blue Shirt', 'Nice blue shirt', 2, '2012-12-15 10:40:10');
/*!40000 ALTER TABLE `clothes` ENABLE KEYS */;
/*!40014 SET FOREIGN_KEY_CHECKS=1 */;
/*!40101 SET CHARACTER_SET_CLIENT=#OLD_CHARACTER_SET_CLIENT */;
And here is the code to retrieve your data:
SELECT
clothes.id AS clotheId,
clothes.item AS clotheItem,
clothes.description AS clotheDescription,
categories.id AS categoryId,
categories.name AS categoryName
FROM clothes
LEFT JOIN categories
ON clothes.category_id=categories.id;
I'd go with the latter.
ID | Item | Description | Price | Type
Then use a query like:
SELECT * FROM items WHERE type='$type'
I would not store this data in separate tables. Create one table, then include an indicator as to the type of item it is.
Similar to this:
create table ClothingItems
(
id,
typeid,
item,
description,
price
);
create table clothingType
(
typeid,
name
);
Then to query you can use:
select *
from clothingitems i
left join clothingType t
on t.typeid = t.typeid
where t.name = 'socks'
Well, it might not have so many entrees for the moment. But maybe in the future? Don't put yourself in a corner :).
Ask yourself the question; -Can an article belong to more than one category?
If NO, you should create at least 2 tables (one-to-many relation):
categories
id|title|description|...
articles
id|category_id|title|price|...
If YES, you should create at least 3 tables (many-to-many relation):
categories
id|title|description|...
articles
id|category_id|title|price|...
articles_categories
article_id|category_id
Best of luck :)