Searching String In All Tables And Displaying It With Pagination Technique - php

My database has five tables:
Computer|Mobile|Tablet|MusicSystem|Camera
All the tables are in same structure like:
productID|ProductBrand|Price|userId
Here, I want to search product's brand name in all the field of productBrand in all the tables, where userId=$userId and limit is 10 then display it with pagination technique.
How can I create such a query in MySQLi and display it in PHP?
Thanks in advance.

try this...
select * from Computer c,Mobile m,Tablet t,MusicSystem ms,Camera cam where userId = $userId and (c.productBrand = searchingProductBrand or m.productBrand = searchingProductBrand or t.productBrand = searchingProductBrand or ms.productBrand = searchingProductBrand or cam.productBrand = searchingProductBrand ) limit 10

If you are following same structure for each category table,better structure is to categorize all into single table as follows
CREATE TABLE IF NOT EXISTS `category` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`category_name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `category` (`id`, `category_name`) VALUES
(1, 'Computer'),
(2, 'Mobile'),
(3, 'Tablet'),
(4, 'MusicSystem'),
(5, 'Camera');
CREATE TABLE IF NOT EXISTS `products` (
`ProductId` int(11) NOT NULL AUTO_INCREMENT,
`ProductBrand` varchar(255) NOT NULL,
`Price` varchar(100) NOT NULL,
`UserId` int(11) unsigned NOT NULL,
`CatId` int(11) unsigned NOT NULL,
PRIMARY KEY (`ProductId`),
KEY `CatId` (`CatId`),
CONSTRAINT `products_ibfk_1` FOREIGN KEY (`CatId`) REFERENCES `category` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
INSERT INTO `products` (`ProductId`, `ProductBrand`, `Price`, `UserId`, `CatId`) VALUES
(1, 'Lenovo', '35000', 5, 3),
(2, 'Asus', '28350', 5, 2),
(3, 'Dell', '25000', 5, 3),
(4, 'MotoG', '12500', 5, 2),
(5, 'Samsung', '52000', 4, 1);
You can get your result using
SELECT * FROM products WHERE userId = '5'
Check the FIDDLE

Related

How could i display the items from 3 tables joined

How could i display the items from 3 tables below joining using the product_tbl id.
and using foreach. or if there is any alternative way to display it.
The tables
I couldn't get the 3rd table or pictures table.
using my existing code below.
still error.
$product_list = DB::table('product_tbl')
->leftjoin('brand_tbl', 'product_tbl.id', '=', 'brand_tbl.product_id')
->select('*')
->get();
return vehicles_list;
$prod_list = array();
foreach ($product_list as $key => $value) {
$prod_list [$value->products_name][] = $value;
}
print_r($prod_list);
please help me thank you.
If your table structure isn't set in stone you might try the following structure for a more normalized and indexable structure. I made some changes - changing product to category, and brand to product. These labels made more sense to me as I worked with this. I also do full namespaced field names to make joining possible without aliasing all field names.
see the bottom of this post for the SQL to generate and populate these tables
Then a different query will definitely help. You're not joining on the picture table in your code.
select * from so_category
left outer join so_product on ( product_category_id = category_id)
left outer join so_picture on ( picture_category_id = category_id )
But this will probably not return your data like you want to consume it.
In which case the Mysql Group Concat Function might help.
SELET
so_category.*,
so_product.*,
(select GROUP_CONCAT( DISTINCT picture_url order by picture_url asc separator "," )) AS images
FROM so_category
LEFT OUTER JOIN so_product ON ( product_category_id = category_id)
LEFT OUTER JOIN so_picture ON ( picture_category_id = category_id )
GROUP BY product_id
Which yields results that do not duplicate data.
SQL to create and populate tables.
-- Create syntax for TABLE 'so_category'
CREATE TABLE `so_category` (
`category_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`category_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB CHARSET=utf8;
-- Create syntax for TABLE 'so_picture'
CREATE TABLE `so_picture` (
`picture_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`picture_url` varchar(100) DEFAULT NULL,
`picture_category_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`picture_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Create syntax for TABLE 'so_product'
CREATE TABLE `so_product` (
`product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`product_name` varchar(100) DEFAULT NULL,
`product_brand_id` int(11) unsigned DEFAULT NULL,
`product_category_id` int(11) unsigned DEFAULT NULL,
PRIMARY KEY (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `so_category` (`category_id`, `category_name`)
VALUES
(1, 'chocolate'),
(2, 'flower');
INSERT INTO `so_picture` (`picture_id`, `picture_url`, `picture_category_id`)
VALUES
(1, 'dmc1', 1),
(2, 'dmc2', 1),
(3, 'dmc3', 2),
(4, 'dmc4', 2),
(5, 'dmc5', 2);
INSERT INTO `so_product` (`product_id`, `product_name`, `product_brand_id`, `product_category_id`)
VALUES
(1, 'hershey', NULL, 1),
(2, 'tobleron', NULL, 1),
(3, 'oreo', NULL, 1),
(4, 'roses', NULL, 2),
(5, 'sunflower', NULL, 2);

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}';";

hand-coded table name works but dynamic name is not working in PHP

This is the code That is supposed to delete the student from the table.
but it it not working.
it only works when the table name is static.
public function delete_student($student_class,$school_session,$id){
$table_name = $student_class."_".$school_session."_student_record";
$pdo = KITE::getInstance('pdo');
$query = $pdo->prepare("DELETE FROM ".$table_name." WHERE `id` = ?");
$query->bindValue(1,$id);
$query->execute();
}
As I mentioned in a comment, a slash / is not permitted in the name of a table, column, function name, procedure name etc. Why then your query works when the name is statically typed is a mystery.
A quickly constructed demo that uses innodb tables with foreign keys and hopefully very little / no redundancy. If you have mysql running locally try running this in your gui ( such as heidi ) to get an idea of how it might be accomplished.
drop database if exists `demo_school_sessions`;
create database if not exists `demo_school_sessions`;
use `demo_school_sessions`;
drop table if exists `classes`;
create table if not exists `classes` (
`id` int(10) unsigned not null auto_increment,
`class` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `classes` (`id`, `class`) values
(1, 'mathematics'),
(2, 'physics'),
(3, 'computing');
drop table if exists `sessions`;
create table if not exists `sessions` (
`id` int(10) unsigned not null auto_increment,
`session` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `sessions` (`id`, `session`) values
(1, 'summer'),
(2, 'winter'),
(3, 'spring'),
(4, 'autumn');
drop table if exists `students`;
create table if not exists `students` (
`id` int(10) unsigned not null auto_increment,
`student` varchar(50) not null,
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `students` (`id`, `student`) values
(1, 'bobby droptables'),
(2, 'sue select'),
(3, 'david delete'),
(4, 'valerie view'),
(5, 'peter procedure'),
(6, 'freddy function');
drop table if exists `year`;
create table if not exists `year` (
`id` int(10) unsigned not null auto_increment,
`year` varchar(9) not null default '2014/2015',
primary key (`id`)
) engine=innodb default charset=latin1;
insert into `year` (`id`, `year`) values
(1, '2014/2015'),
(2, '2015/2016'),
(3, '2016/2017'),
(4, '2017/2018'),
(5, '2018/2019'),
(6, '2019/2020');
drop table if exists `master`;
create table if not exists `master` (
`id` int(10) unsigned not null auto_increment,
`student` int(10) unsigned not null default '0',
`class` int(10) unsigned not null default '0',
`session` int(10) unsigned not null default '0',
`year` int(10) unsigned not null default '0',
primary key (`id`),
key `student` (`student`),
key `class` (`class`),
key `session` (`session`),
key `year` (`year`),
constraint `fk_year` foreign key (`year`) references `year` (`id`) on delete cascade on update cascade,
constraint `fk_class` foreign key (`class`) references `classes` (`id`) on delete cascade on update cascade,
constraint `fk_session` foreign key (`session`) references `sessions` (`id`) on delete cascade on update cascade,
constraint `fk_stud` foreign key (`student`) references `students` (`id`) on delete cascade on update cascade
) engine=innodb default charset=latin1;
insert into `master` (`id`, `student`, `class`, `session`, `year`) values
(1, 1, 3, 4, 1),
(2, 1, 1, 4, 1),
(3, 2, 2, 4, 1),
(4, 2, 3, 4, 1);
/* example to select records */
select * from `master` m
left outer join `students` s on s.id=m.student
left outer join `classes` c on c.id=m.class
left outer join `sessions` ss on ss.id=m.`session`
left outer join `year` y on y.id=m.`year`
where m.`class`=3 or m.`student`=1;
/* Example to delete a student */
delete from `students` where `id`=1;
/*
*/
Because of Foreign Key Dependencies if you delete a student from the student table all associated records from the master table would also be deleted as there is a cascade set for both update and delete. Care MUST be taken when using foreign keys like this because if you delete a record accidentally and there is a cascaded delete set on the key then all the associated records disappear!

php/mysql how to join three tables

I'm writing code for a school transcript system. However, I'm stuck in joining the tables which are the courses table I named 'causes', courses registered table I named 'courses_registered' and the students table 'students' actually the join works, but the problem is that data are repeated in multiples depending on how many data rows that are on the courses_registered table for example, if have 2 courses registered, I get 4 rows echoed out from all tables instead of two. so my question is, how can i join these three tables perfectly without data being repeated, when i loop through the data. this is the table structure for the three tables.
--
-- Table structure for table `courses`
--
CREATE TABLE IF NOT EXISTS `courses` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`course_code` varchar(255) NOT NULL,
`course_title` varchar(255) NOT NULL,
`cl` int(255) NOT NULL,
`level` int(255) NOT NULL,
`session` varchar(255) NOT NULL,
`semester` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `courses`
--
INSERT INTO `courses` (`id`, `course_code`, `course_title`, `cl`, `level`, `session`, `semester`) VALUES
(1, 'GSS 111', 'Use of English', 3, 1, '2009/2010', '1'),
(2, 'GSS 112', 'Nigerian History', 2, 1, '2009/2010', '1');
--
-- Table structure for table `courses_registered`
--
CREATE TABLE IF NOT EXISTS `courses_registered` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`course_id` int(255) NOT NULL,
`student_id` int(255) NOT NULL,
`score` int(255) NOT NULL,
`grade` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `courses_registered`
--
INSERT INTO `courses_registered` (`id`, `course_id`, `student_id`, `score`, `grade`) VALUES
(1, 1, 2, 60, 'B'),
(2, 2, 2, 80, 'A');
-- --------------------------------------------------------
--
-- Table structure for table `students`
--
CREATE TABLE IF NOT EXISTS `students` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`regno` varchar(255) NOT NULL,
`college` varchar(255) NOT NULL,
`dept` varchar(255) NOT NULL,
`level` varchar(255) NOT NULL,
`fname` varchar(255) NOT NULL,
`lname` varchar(255) NOT NULL,
`no_of_semesters` int(255) NOT NULL,
`gender` varchar(255) NOT NULL,
`dob` varchar(255) NOT NULL,
`age` int(255) NOT NULL,
`mstatus` varchar(255) NOT NULL,
`nationality` varchar(255) NOT NULL,
`religion` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`phone` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table `students`
--
INSERT INTO `students` (`id`, `regno`, `college`, `dept`, `level`,
`fname`, `lname`, `no_of_semesters`, `gender`, `dob`, `age`,
`mstatus`, `nationality`, `religion`, `address`, `email`, `phone`)
VALUES
(2, 'MOUAU 09/14508', 'CEET', 'Electrical Electronics Engineering',
'1', 'Nnamdi', 'Okoro', 2, 'Male', '24-07-1989', 25, 'Single',
'Nigerian', 'christian', 'Okpu Umiobo road, Aba',
'nokoro#gmail.com', '09056733333');
Then this is the query
<?php
$sql = "
SELECT c.id as course_id
, c.course_code
, c.course_title
, c.cl
, c.level
, c.session
, c.semester
, r.id as rid
, r.course_id
, r.student_id
, r.score
, r.grade
, s.id
, s.regno
, s.fname
, s.lname
, s.no_of_semesters
FROM courses_registered r
JOIN courses c
ON r.course_id = course_id
JOIN students s
ON s.id = r.student_id
WHERE s.id = '$id'
";
$result = mysqli_query($con,$sql) or die ('Could not show students records.'. mysqli_error($con) );
if (mysqli_num_rows($result) > 0 ){
//echo mysqli_num_rows($result);
while($row = mysqli_fetch_array($result) ){
$score[] = $row['score'];
$course_code[] = $row['course_code'];
$course_title[] = $row['course_title'];
$cl[] = $row['cl'];
$grade[] = $row['grade'];
}
Try change this
ON r.course_id = course_id
to
ON r.course_id = c.id

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