MySQL Insert with skipping duplicate values - php

I am new to MySQL. I am getting a user phonebook from their mobile device, and I am saving into a database.
Table Phonebook (User ID is INT)
INSERT INTO `Phonebook`
(`user_id`, `first_name`, `mobile`)
VALUES(4,'tom','+919810012345');
It works fine, but if user again upload his phonebook then I want to skip users existing contacts and add only new one from his/her phonebook and the data can be compare with user_id, first_name, mobile and these columns can't be duplicate.
If the user has +919810012345 and the next time 9810012345/9810-0123-45 then add again to the database.
I tried this, but it's not working:
INSERT Phonebook (`user_id`, `first_name`, `mobile`)
Values (SELECT `user_id`, `first_name`, `mobile`
FROM Phonebook
where `user_id` != $user_id
AND `first_name` !=$name
AND `mobile` != $phn)
Table Schema
CREATE TABLE `Phonebook` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`first_name` varchar(250) NOT NULL,
`mobile` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3520 DEFAULT CHARSET=latin1
Phone Book Table
Table Name: PhoneBook
id user_id first_name mobile
1 100 John +91981000000
2 100 Tom 91981000001
3 100 Ron 9810-000-02
4 100 Mat 91981000003
5 100 Miley 981000004

Add a UNIQUE constraint
CREATE TABLE `Phonebook` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL,
`first_name` varchar(250) NOT NULL,
`mobile` varchar(250) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`user_id`, `first_name`, `mobile`)
) ENGINE=InnoDB AUTO_INCREMENT=3520 DEFAULT CHARSET=latin1
INSERT INTO `Phonebook`
(`user_id`, `first_name`, `mobile`)
VALUES(4,'tom','+919810012345');
SELECT * FROM `Phonebook`
id
user_id
first_name
mobile
3520
4
tom
+919810012345
INSERT INTO `Phonebook`
(`user_id`, `first_name`, `mobile`)
VALUES(4,'tom','+919810012345')
ON DUPLICATE KEY UPDATE user_id = user_id;
SELECT * FROM `Phonebook`
id
user_id
first_name
mobile
3520
4
tom
+919810012345
fiddle

Related

mysql update table field from another table

i have these set of tables
CREATE TABLE `staff` (
`id` mediumint(8) NOT NULL AUTO_INCREMENT,
`uid` varchar(128) NOT NULL,
`name` varchar(128) NOT NULL,
`deptname` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `staff` (`id`, `uid`, `name`, `deptname`) VALUES
(1,'A100','John','Finance'),
(2,'A101','Joana','ICT'),
(3,'A103','Darrel','Maintenance'),
(4,'A104','Smith','HR');
CREATE TABLE `department` (
`id` mediumint(8) NOT NULL AUTO_INCREMENT,
`deptid` int(11) UNSIGNED NOT NULL DEFAULT '0',
`deptname` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `department` (`id`, `deptid`, `deptname`) VALUES
(1,'100','ICT'),
(2,'200','Finance'),
(8,'300','HR'),
(11,'400','Maintenance'),
(12,'500','Backup');
CREATE TABLE `new_staff` (
`id` mediumint(8) NOT NULL AUTO_INCREMENT,
`uid` varchar(128) NOT NULL,
`name` varchar(128) NOT NULL,
`deptid` int(11) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `new_staff` (`id`, `uid`, `name`, `deptid`) VALUES
(1,'A100','John','600'),
(2,'A103','Darrel','400'),
(3,'A104','Smith','300'),
(4,'A101','Joana','500'),
(5,'A105','Fran','800');
i would like to update the deptid field in the new_staff table to have the correct deptid as listed in the department table
in the new_staff table deptid are currently wrong for John and Joana
this is what i tried so far
// list all deptid exists
$result=$mysqli->query("SELECT deptid FROM department");
while ($rows=mysqli_fetch_array($result))
{$deptid[]=$rows['deptid'];}
$deptidarray = implode(', ', $deptid);
echo "<br>";
//echo "$deptidarray<br>";
$query=$mysqli->query("SELECT deptid from new_staff WHERE deptid not in ($deptidarray)");
echo "<br>";
$rowtotal = mysqli_num_rows($query);
if($rowtotal>0){
while ($row=mysqli_fetch_array($query))
{
$deptid=$row['deptid'];
// echo "$deptid,";
//$query=$mysqli->query("UPDATE new_staff set deptid='$deptid' WHERE ");
}
}
else
{
// not found
}
is this possible to do entirely in mysql?
UPDATE new_staff AS ns
JOIN staff AS s ON
ns.uid = s.uid // 1. Get same users from 2 tables
JOIN department AS d ON
s.deptname = d.deptname // 2. Get department
SET ns.deptid = d.deptid // 4. Update correct department id
WHERE ns.deptid != d.deptid; // 3. Get users where department is incorrect
Run SELECT query first before updating to verify the updates.
SELECT ns.*, d.deptid AS new_dept_id
FROM new_staff AS ns
JOIN staff AS s ON
ns.uid = s.uid
JOIN department AS d ON
s.deptname = d.deptname
WHERE ns.deptid != d.deptid;

Insert Data From One Table to Another Table and Add New Values

I have two tables.
Temporary table :
CREATE TABLE IF NOT EXISTS `temporary` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`FK_user` int(11) NOT NULL,
`FK_bin` varchar(50) NOT NULL,
`orderDate` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=35 ;
And orders table :
CREATE TABLE IF NOT EXISTS `orders` (
`id` int(11) NOT NULL,
`FK_user` int(11) NOT NULL,
`FK_bin` varchar(50) NOT NULL,
`orderNumber` varchar(11) NOT NULL,
`orderDate` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I want to insert all values of Temporary table to Orders table and add orderNumber manually with this : uniqid(rand()) .
I have use INSERT INTO orders SELECT * FROM temporary WHERE FK_user = ?
But they don't work because orderNumber don't exist in Temporary table ...
How I can do ? please
Hoping i understand your questioin correctly. I think below query will help.
INSERT INTO orders(id , FK_user , FK_bin , orderNumber , orderDate)
(SELECT id , FK_user , FK_bin , uniqid(rand()) AS orderNumber , orderDate
FROM temporary WHERE FK_user = ?);
Use your function to generate order number in select statement as mentioned in above query.

Find Tag with field_in_set over 3 Tables without Join

I have 3 tables :
link_art_a
link_art_b
link_art_c
I want to now search for "car" and this output on one side .
The tables are almost identical , fields that I need :
id, name , mod_name , picture , hits.
The field is where there wanted , is separated by a comma , for example :
Car , truck , SUV , bike
Field: "tag"
I've FIND_IN_SET attempts , but I always get all the entries in the database and not with the "car" .
What I have tried so far :
SELECT id, name, mod_name, picture, hits from link_art_a
UNION ALL
SELECT id, name, mod_name, picture, hits from link_art_b
UNION ALL
SELECT id, name, mod_name, picture, hits from link_art_c
where
find_in_field('Car', tag) > 0 order by name asc
Does anyone have an idea how I get my desired result ?
Edit:
Hello,
The problem is that I get all records including those that do not contain the desired word such as "car" .
The MySQL Dump:
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
CREATE TABLE `link_art_a` (
`id` bigint(20) NOT NULL,
`name` varchar(100) NOT NULL,
`mod_name` varchar(200) NOT NULL,
`picture` varchar(100) NOT NULL,
`hits` bigint(20) NOT NULL,
`tag` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `link_art_a` (`id`, `name`, `mod_name`, `picture`, `hits`, `tag`) VALUES
(1, 'A8', 'a8.html', 'default.jpg', 251, 'car,sports car,sport,fast'),
(2, 'VW Beetle', 'vw-beetle', 'default.jpg', 269, 'car,fun,slow');
CREATE TABLE `link_art_b` (
`id` bigint(20) NOT NULL,
`name` varchar(100) NOT NULL,
`mod_name` varchar(200) NOT NULL,
`picture` varchar(100) NOT NULL,
`hits` bigint(20) NOT NULL,
`tag` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `link_art_b` (`id`, `name`, `mod_name`, `picture`, `hits`, `tag`) VALUES
(1, 'Surfboard', 'surfboard', 'default.jpg', 142, 'fun,sport,water'),
(2, 'Sport boat', 'sport-boat', 'default.jog', 163, 'sport,fun,water,fast');
CREATE TABLE `link_art_c` (
`id` bigint(20) NOT NULL,
`name` varchar(100) NOT NULL,
`mod_name` varchar(200) NOT NULL,
`picture` varchar(100) NOT NULL,
`hits` bigint(20) NOT NULL,
`tag` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `link_art_c` (`id`, `name`, `mod_name`, `picture`, `hits`, `tag`) VALUES
(1, 'Houseboat', 'houseboat', 'default.jog', 144, 'house,boat,water'),
(2, 'Speedboat', 'speedboat', 'default.jpg', 142, 'water,boot,speed,fast');
ALTER TABLE `link_art_a` ADD PRIMARY KEY (`id`), ADD KEY `tag` (`tag`);
ALTER TABLE `link_art_b` ADD PRIMARY KEY (`id`), ADD KEY `tag` (`tag`);
ALTER TABLE `link_art_c` ADD PRIMARY KEY (`id`), ADD KEY `tag` (`tag`);
ALTER TABLE `link_art_a`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
ALTER TABLE `link_art_b`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
ALTER TABLE `link_art_c`
MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;COMMIT;
your where clause applies to the last select only.
You have to add where to all select statements
Like this:
SELECT id, name, mod_name, picture, hits from link_art_a
where
find_in_set('Car', tag) > 0
UNION ALL
SELECT id, name, mod_name, picture, hits from link_art_b
where
find_in_set('Car', tag) > 0
UNION ALL
SELECT id, name, mod_name, picture, hits from link_art_c
where
find_in_set('Car', tag) > 0
NOTES:
you must remove order by in this case.
it is find_in_set() not find_in_field()
Or add it to all of them
SELECT id, name, mod_name, picture, hits from
(
SELECT id, name, mod_name, picture, hits, tag from link_art_a
UNION ALL
SELECT id, name, mod_name, picture, hits, tag from link_art_b
UNION ALL
SELECT id, name, mod_name, picture, hits, tag from link_art_c
) as t1
where
find_in_set('Car', t1.tag) > 0 order by t1.name asc

How can I query data from multiple tables connected through foreign keys?

I have a Search page where users can search based on certain criteria. I cannot get the query right to display only one of the desired objects. It is displaying multiple entries for specified criteria. Here is our database setup:
CREATE TABLE Class
(
cid int NOT NULL,
classNum int,
classDept varchar(20),
PRIMARY KEY (cid)
);
CREATE TABLE Book
(
bid int NOT NULL,
cid int NOT NULL,
title varchar(20) NOT NULL,
author varchar(20) NOT NULL,
isbn varchar(13),
price decimal(5,2) NOT NULL,
PRIMARY KEY (bid),
FOREIGN KEY (cid) REFERENCES Class (cid)
);
CREATE TABLE Contact
(
contactID int NOT NULL,
fname varchar(20),
lname varchar(20),
contactInfo varchar(90) NOT NULL,
PRIMARY KEY (contactID)
);
CREATE TABLE Post
(
pid int NOT NULL,
contactID int NOT NULL,
bid int NOT NULL,
postDate date,
PRIMARY KEY (pid),
FOREIGN KEY (contactID) references Contact(contactID),
FOREIGN KEY (bid) references Book(bid)
);
Here is the php code we are using to try to query based on criteria the user has entered.
$author = $_POST["author"];
$title = $_POST["title"];
$classNum = $_POST["number"];
$classDept = $_POST["department"];
$isbn = $_POST["isbn"];
$query = "SELECT title, author, isbn, price, classNum, classDept, contactInfo
FROM Book, Class, Contact, Post
WHERE Book.cid=Class.cid AND Contact.contactID=Post.ContactID and Book.bid=Post.bid AND Book.author='$author' OR Book.title='$title' ";
$stid = oci_parse($conn,$query);
oci_execute($stid,OCI_DEFAULT);
What are we doing wrong? How can we get the proper search results to display?
I'm not entirely sure that the join syntax in Oracle is the same as mysql ( which is what this was built and tested in ) but given the dummy data and minor modifications to some tables ( adding auto_increment to primary keys ) it returns the expected two rows that match the params.
create table if not exists `book` (
`bid` int(11) not null auto_increment,
`cid` int(11) not null,
`title` varchar(64) not null,
`author` varchar(20) not null,
`isbn` varchar(13) default null,
`price` decimal(5,2) not null,
primary key (`bid`),
key `cid` (`cid`),
constraint `book_ibfk_1` foreign key (`cid`) references `class` (`cid`)
) engine=innodb auto_increment=5 default charset=utf8;
insert into `book` (`bid`, `cid`, `title`, `author`, `isbn`, `price`) values
(1, 1, 'deranged dahlias', 'gertrude geikel', '1234-cdbs-9d', 250.00),
(2, 1, 'mental magnolias', 'gertrude geikel', '4234-cdfg-9f', 225.00),
(3, 1, 'raging roses', 'gertrude geikel', '4389-fkpl-8d', 120.25),
(4, 3, 'peaceful petunias', 'alice cooper', '9043-dflk-01', 500.25);
create table if not exists `class` (
`cid` int(11) not null auto_increment,
`classnum` int(11) default null,
`classdept` varchar(20) default null,
primary key (`cid`)
) engine=innodb auto_increment=4 default charset=utf8;
insert into `class` (`cid`, `classnum`, `classdept`) values
(1, 404, 'fookology'),
(2, 403, 'forbidden fruits'),
(3, 200, 'goodness');
create table if not exists `contact` (
`contactid` int(11) not null auto_increment,
`fname` varchar(20) default null,
`lname` varchar(20) default null,
`contactinfo` varchar(90) not null,
primary key (`contactid`)
) engine=innodb auto_increment=3 default charset=utf8;
insert into `contact` (`contactid`, `fname`, `lname`, `contactinfo`) values
(1, 'joe', 'bloggs', 'chief headbanger'),
(2, 'fred', 'flintstone', 'potatopeeler');
create table if not exists `post` (
`pid` int(11) unsigned not null auto_increment,
`contactid` int(11) not null,
`bid` int(11) not null,
`postdate` date default null,
primary key (`pid`),
key `contactid` (`contactid`),
key `bid` (`bid`),
constraint `post_ibfk_1` foreign key (`contactid`) references `contact` (`contactid`),
constraint `post_ibfk_2` foreign key (`bid`) references `book` (`bid`)
) engine=innodb auto_increment=3 default charset=utf8;
insert into `post` (`pid`, `contactid`, `bid`, `postdate`) values
(1, 1, 1, '2015-12-02'),
(2, 2, 4, '2015-12-02');
Query run in mysql gui - not sure if the join syntax is the same in oracle .
set #_author='alice cooper';
set #_title='mental magnolias';
select * from `book` b
left outer join `class` c on c.`cid`=b.`cid`
left outer join `post` p on p.`bid`=b.`bid`
left outer join `contact` ct on ct.`contactID`=p.`contactID`
where b.`title`=#_title or b.`author`=#_author;
Or, for php
$author=$_POST['author'];
$title=$_POST['title'];
$sql="select * from `book` b
left outer join `class` c on c.`cid`=b.`cid`
left outer join `post` p on p.`bid`=b.`bid`
left outer join `contact` ct on ct.`contactID`=p.`contactID`
where b.`title`='{$title}' or b.`author`='{$author}';";

Advanced many2many query for MYSQL

I im trying to build a imagegallery where people have access to different groups and the groups decide what catalogues and images they are allowed to see.
I though many2many structure would be best for this.
So far, ive manage to build the database like this:
image (image_name, image_file, image_id)
catalog (catalog_id, catalog_name)
supplier (supplier_id, supplier_name)
user (name, user_id)
image2cataloge (image_id, catalog_id)
image2supplier (image_id, supplier_id)
catalog2supplier (catalog_id, supplier_id)
user2supplier (user_id, supplier_id)
So... that been said, saving images and making supplier (or group if you want), adding users to supplier and linking images to supplier and catalogues is no problem. Inserting is no problem.
But selecting the right images based upon the users supplier setting and catalog they are in is harder.
For example, I have a user with user_id 1, which have access to supplier_id 1. supplier_id 1 have access to view catalogue 1, which holds images with image_id 1 and 2.
But supplier_id 1 only have access to image_id 2.
All this settings are stored in the database. How do I do the select query?
This is what i've tested;
//$catalog_id is the catalog_id we are in
//$user_id is the current users user_id
$sql = "SELECT i.*
FROM image i, user u, catalog cat, supplier s, supplier2user s2u, supplier2catalog s2c, image2catalog i2c, image2supplier i2s
WHERE u.id = '".$user_id."'
AND s2u.user_id = '".$user_id."'
AND s2u.supplier_id = s.id
AND s2c.catalog_id = '".$catalog_id."'
AND i2c.catalog_id = '".$catalog_id."'
AND i2s.supplier_id = s.id
AND s2c.supplier_id = s.id
GROUP BY i.id
ORDER BY i.name ASC
But when ive added more than one image, all images are shown for all users in all catalogues.
EDIT (2010/02/05):
Okey, so I've figured out how to at least show correct images in correct catalog. I do this by doing following:
$sql = "SELECT i.*
FROM
image i
INNER JOIN image2catalog i2c
ON i.id = i2c.image_id
AND i2c.catalog_id = '".$pages_level_0[$i]['id']."'
GROUP BY i.id
;";
This let's me output the correct images that belongs in the catalog the user is visiting at the moment. Now I just need to edit this query to filter out all images the user doesn't have access to. I very grateful for any help you can provide!
EDIT 2010/02/09:
---
CREATION SCHEME
CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`email` varchar(250) NOT NULL,
`password` varchar(50) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) character set utf8 NOT NULL default '',
`url` varchar(250) character set utf8 NOT NULL default '',
`childof` varchar(250) character set utf8 NOT NULL default '',
`hide` tinyint(4) NOT NULL,
`publishdate` varchar(14) NOT NULL,
`expiredate` varchar(14) NOT NULL,
`editdate` varchar(14) NOT NULL,
`editbygroup` varchar(250) NOT NULL,
`openby` varchar(250) NOT NULL,
`opendate` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;
CREATE TABLE `imagebank_image` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) NOT NULL,
`img` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_image2catalog` (
`image_id` int(11) NOT NULL,
`catalog_id` int(11) NOT NULL,
PRIMARY KEY (`image_id`,`catalog_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_image2supplier` (
`image_id` int(11) NOT NULL,
`supplier_id` int(11) NOT NULL,
PRIMARY KEY (`image_id`,`supplier_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier2catalog` (
`supplier_id` int(11) NOT NULL,
`catalog_id` int(11) NOT NULL,
PRIMARY KEY (`supplier_id`,`catalog_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `imagebank_supplier2user` (
`supplier_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
PRIMARY KEY (`supplier_id`,`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
SOME DATA:
INSERT INTO `imagebank` (`id`, `name`, `url`, `childof`, `hide`, `publishdate`, `expiredate`, `editdate`, `editbygroup`, `openby`, `opendate`) VALUES
(1, 'Test 1', 'test-1', '', 0, '20100204230233', '', '', '', '', ''),
(2, 'Test 2', 'test-2', '', 0, '20100204230244', '', '', '', '', '');
INSERT INTO `imagebank_image` (`id`, `name`, `img`) VALUES
(1, 'Test img 1', 'labb_9noq80bik5.jpeg'),
(2, 'Test img 2', 'labb_53626114dz.jpeg');
INSERT INTO `imagebank_image2catalog` (`image_id`, `catalog_id`) VALUES
(1, 1),
(2, 2);
INSERT INTO `imagebank_image2supplier` (`image_id`, `supplier_id`) VALUES
(1, 2),
(2, 1);
INSERT INTO `imagebank_supplier` (`id`, `name`) VALUES
(1, 'Supplier1'),
(2, 'Supplier2'),
(3, 'Supplier3');
INSERT INTO `imagebank_supplier2catalog` (`supplier_id`, `catalog_id`) VALUES
(1, 2),
(2, 1);
INSERT INTO `imagebank_supplier2user` (`supplier_id`, `user_id`) VALUES
(1, 1),
(1, 11),
(1, 12),
(2, 1),
(2, 10),
(3, 1);
INSERT INTO `user` (`id`, `email`, `password`) VALUES
(1, 'User1#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(10, 'User2#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(11, 'User3#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444'),
(12, 'User4#test.com', 'ff02dd5s33taa2ba5ff7c2c4d3327e444');
WOW, now thats alot of stuff :P So I know that the tables, specially for "catalogs" which i call just "imagebank" might look abit strange. But I do have my reasons and thats not really the issue :) Its part of an even bigger picture. Hope this helps you to help me. Thanks again.
It looks like if you are passing in the user and catalogue id's then the supplier doesn't matter.
If you required the supplier information in the result, that would be a different matter.
It feels like you shouldn't be involving the user in this query at all as you seem to be looking for the images in a catalogue that are owned by a particular supplier.
If that is the case, then I would drop the requirement for the user id in the query and use the supplier id instead.
I am assuming that the user would have done the following to get to the point where they would be initiating this query:
login - obviously :)
click on 'list suppliers'
click on a supplier
click on a catalog
Either way you are going to have to do a lot of INNER JOIN's. For instance the query to retrieve the list of suppliers for a given user would be something like
SELECT
s.supplier_id,
s.Supplier_name
FROM
supplier s
INNER JOIN
user u
INNER JOIN
user2supplier u2s
ON
u.user_id = u2s.user_id
ON
u2s.supplier_id = s.supplier_id
WHERE
u.user_id = 3 -- for example...
(now, I haven't tested the SQL, but I think that is right...)
Let me know if I'm on the right track - if I have helped, I'd be happy to help some more if I can

Categories