I need to output A_Name, B_Name, and C_Name. from tableA, tableB, tableC. I have a linking table containing all the ID's of the above, e.g:
CREATE TABLE `tableLink` (
`tableLinkID` int(5) NOT NULL auto_increment,
`A_ID` int(11) NOT NULL,
`B_ID` int(5) NOT NULL,
`C_ID` int(5) NOT NULL,
PRIMARY KEY (`tableLinkID`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8 AUTO_INCREMENT=17 ;
My question: I need to know how to SELECT based on having C_ID, how to select and output A_ID's [A_Name], B_ID's [B_Name], and C_ID's [C_Name].
I hope this is clear enough.
(I have the C_ID in a variable)
Try using JOINs:
SELECT
tableA.A_Name,
tableB.B_Name,
tableC.C_Name
FROM tableLink
JOIN tableA ON tableLink.A_ID = A.ID
JOIN tableB ON tableLink.B_ID = B.ID
JOIN tableC ON tableLink.C_ID = C.ID
WHERE tableLink.C_ID = 42
Related
create table area (
area_id int(11) not null auto_increment,
area_name varchar(60) not null,
primary key (area_id)
);
create table mainCategory(
mc_id int(11) not null auto_increment,
mc_name varchar(60) not null,
area_id int(11) not null,
primary key(mc_id),
foreign key(area_id) references area(area_id)
);
create table subCategory(
sc_id int(11) not null auto_increment,
sc_name varchar(60) not null,
mc_id int(11) not null,
area_id int(11) not null,
primary key(sc_id),
foreign key(mc_id) references mc(mc_id),
foreign key(area_id) references area(area_id)
);
create table shopes(
s_id int(11) not null auto_increment,
s_name varchar(60) not null,
s_address varchar(120) not null,
s_work varchar(255) not null,
s_imagepath varchar(255) not null,
s_image varchar(255) not null,
area_id int(11) not null,
mc_id int(11) not null,
sc_id int(11) not null,
primary key(s_id),
foreign key(area_id) references area(area_id),
foreign key(mc_id) references mc(mc_id),
foreign key(sc_id) references sc(sc_id)
);
I want to select data from four table with mysql join. I am using
select s_name,s_address,s_work,s_image,area_name,mc_name from shopes inner join area on area.area_id=shopes.area_id inner join mainCategory on mc.area_id=area.area_id;
It is a three table join and it is not giving appropriate result. It is giving repeated result.
Try this:
select s.s_name, s.s_address, s.s_work, s.s_image, a.area_name, mc.mc_name from shope as s LEFT JOIN area as a on a.area_id=s.area_id LEFT JOIN mainCategory as mc on mc.area_id=a.area_id LEFT JOIN subCategory as sc on sc.area_id=a.area_id
I have used LEFT JOIN keyword instead of inner join, it's returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.
Use this syntax.
select a.coumn1,
a.coumn2,
b.coumn1,
c.coulumn1,
d.column1,
d.column2
from table1 a
left outer join table2 b
on a.id=b.id
left outer join table3 c
on b.id=c.id
left outer join table4 d
on a.id=d.id
where a.name = 'abc'
select s_name, s_address, s_work, s_image, area_name, mc_name
from shopes, area, mainCategory
where shopes.area_id = area.area_id
and shopes.mc_id = mainCategory.mc_id;
If still got duplicate data, use:
select distinct s_name, ...
Try it.
select shopes.s_name,s_address,s_work,s_image, area.area_name, maincategory.mc_name from shopes join area on shopes.area_id=area.area_id
join maincategory on shopes.mc_id=maincategory.mc_id;
select s_name,s_address,s_work,s_image,area_name,mc_name
from area
inner join area
on area.area_id=shopes.area_id
inner join mainCategory
on mc.area_id=area.area_id;
I have 2 tables, table1 has information about the producer (name of producer and their ID) of a resturant.
CREATE TABLE IF NOT EXISTS `fournisseur` (
`IdFournisseur` int(10) NOT NULL auto_increment,
`Fournisseur_Producteur` varchar(250) default NULL,
PRIMARY KEY (`IdFournisseur`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
table2 has dailymanue and the ID of thier producer(for exampe: IdMenuPlat, IdMenuEntree...).
CREATE TABLE IF NOT EXISTS `menu_jours` (
`menu_id` int(10) NOT NULL auto_increment,
`menu_j_titre` varchar(250), `menu_date_parution`,
`menu_entree` int(10),
`IdMenuEntree`,`menu_plat`,`IdMenuPlat`,`menu_acc1`,`IdMenuAcc1`,`menu_acc2`,`IdMenuAcc2`,`menu_sugg`,`IdMenuSugg`,`menu_sugg_acc`,`IdMenuSuggAcc`,`menu_dessert`,`IdMenuDessert` default NULL,
PRIMARY KEY (`menu_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I want to recover daily menu with the name of their producer!
I used this query:
todaySQL = 'SELECT * FROM table2 AS t2
INNER JOIN table1 AS t1_1 ON (t1_1.IdProducter= t2.IdMenuPlat)
INNER JOIN table1 AS t1_2 ON (t1_2.IdProducter= t2.IdMenuEntree)
INNER JOIN table1 AS t1_3 ON (t1_3.IdProducter= t2.IdMenuAcc1)
INNER JOIN table1 AS t1_4 ON (t1_4.IdProducter= t2.IdMenuAcc2)
INNER JOIN table1 AS t1_5 ON (t1_5.IdProducter= t2.IdMenuSugg)
INNER JOIN table1 AS t1_6 ON (t1_6.IdProducter= t2.IdMenuSuggAcc)
INNER JOIN table1 AS t1_7 ON (t1_7.IdProducter= t2.IdMenuDessert)
WHERE menu_date_parution = \''.$todayDate.'\'' ;
but if I use this function:
todayRow = mysql_fetch_row( todaySQL )
I have all information in array by their index which is confusing for programming I would like to use this function:
todayRow = mysql_fetch_assoc( todaySQL )
which recover in array by the name of columns, but the problem is that I just have the infortmaion of the last producer ,not for the information of the all.
I think I should rename the column's name of producer(Fournisseur_Producteur) for each join. But how?
If i understand well, you dont use a loop to fetch the results.
You need a for or a while:
while($row=mysql_fetch_assoc($todaySQL)) {
echo $row['menu_id'];
}
I have two tables in MySQL: t1 AND t2.
The structure is something like that:
t1.aa t2.az
t1.bb t2.zz
t1.cc t2.aa
t2.dz
Now, I need to get something like:
result: t1.aa = 'value', t2.dz = 'value';
BUT:
1) second table (t2) is possibly just empty
2) In my query "WHERE" statement like this:
WHERE t1.aa=t2.aa AND t2.zz=345
So, if second table is really empty I need get something like this:
result: t1.aa = 'value', t2.dz = null;
Thanks.
P.S. we have other answers like Select multiple tables when one table is empty in MySQL , but my problem is in "WHERE" statement..
My tables are:
Table t1
CREATE TABLE IF NOT EXISTS `t1` (
`aa` int(5) NOT NULL AUTO_INCREMENT,
`bb` varchar(56) NOT NULL,
`cc` varchar(56) NOT NULL,
PRIMARY KEY (`aa`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
Table t1 Data
INSERT INTO `t1` (`aa`, `bb`, `cc`) VALUES
(1, 'varchar in bb', 'cc varchar');
Table t2
CREATE TABLE IF NOT EXISTS `t2` (
`az` int(5) NOT NULL AUTO_INCREMENT,
`zz` int(5) NOT NULL,
`aa` int(5) NOT NULL,
`dz` int(10) NOT NULL,
PRIMARY KEY (`az`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Table t2 Data is null
I am not a genius, but I read mysql documentacion
http://dev.mysql.com/doc/#manual
Use JOIN ( I did not Test it) you need to prove it.
SELECT * FROM t1 LEFT JOIN t2 ON t1.aa == t2.aa WHERE t2.zz = 345
Lea Tano, we were close.
The right query is:
SELECT * FROM t1 LEFT JOIN t2 ON t1.aa=t2.aa AND zz=345
As you can see, there is no need in "WHERE" statement. This role has inherited by "ON ... AND ...".
Thank all for any help.
And Lea Tano, mostly.
In my database, I have 3 tables.
Jokes:
CREATE TABLE IF NOT EXISTS `jokes` (
`joke_id` int(11) NOT NULL AUTO_INCREMENT,
`joke` varchar(1024) NOT NULL,
`category_id` int(11) NOT NULL,
`vote` int(255) NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`joke_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Category:
CREATE TABLE IF NOT EXISTS `category` (
`category_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(51) NOT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And finally, Comments:
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(40) NOT NULL,
`comment` text NOT NULL,
`joke_id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
These three tables need to be joined together to get data from each. So far i have used these three queries with no luck in getting what i need.
what i need this query to do is
Assign the joke_id to the joke_id in the comments, and also displays the joke_id in the view.
Assign the category_id of a joke to the name and display it
grab the votes
grab the jokes
1st query - this query does actually grab all the data i want, but at the same time does not grab the joke_id and pass it to view (as i need the joke_id to assign comments to that unique id):
SELECT j.*, c.name,
co.* FROM jokes j LEFT
JOIN category c ON c.category_id
= j.category_id LEFT JOIN
comments co ON co.joke_id =
j.joke_id WHERE j.joke_id = '$joke_id'
2nd query - This query joins the category to the correct one, and displays the joke and joke_id for me to assign comments to. But it does not show any comments
SELECT j.*, c.name
FROM jokes j LEFT JOIN category c
ON c.category_id = j.category_id
WHERE joke_id = '$joke_id'
3rd query - This query was provided by a user on stack overflow, but seems to throw a tonne of errors my way when a there is no comment attatched to that joke
SELECT c.*, j.*, co.*
FROM jokes j
INNER JOIN category c ON c.category_id = j.category_id
INNER JOIN comments co ON co.joke_id = j.joke_id
WHERE j.joke_id = '$joke_id'
Any help altering this query to get all the items in the three database together would be much appreciated!
Edit the selected fields to suits your needs. Also if you want to display only one joke and its data you have to set the id of the joke you want as you did on the WHERE for instance.
SELECT jokes.*, category.*, comments.* FROM jokes LEFT JOIN category ON jokes.category_id = category.category_id LEFT JOIN comments ON jokes.jokes_id = comments.joke_id
I have two tables in this scenario: members and team_members. The members table is pretty self explanatory. The team members table stores the member's team information if they are a member of the team. If there is no row in the team members table that has a member_id of a user, then they are not in a team. What I want to do is get all the users that are not members of a team. Should I use left join, inner join, outer join, or just join? What would this query look like?
CREATE TABLE IF NOT EXISTS `members` (
`member_id` int(15) NOT NULL AUTO_INCREMENT,
`group_id` int(15) NOT NULL,
`display_name` text NOT NULL,
`email_address` text NOT NULL,
`password` text NOT NULL,
`status` tinyint(1) NOT NULL,
`activation_code` varchar(16) NOT NULL,
`date_joined` text NOT NULL,
PRIMARY KEY (`member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `team_members` (
`team_member_id` int(15) NOT NULL AUTO_INCREMENT,
`member_id` int(15) NOT NULL,
`team_id` int(15) NOT NULL,
`date_joined` text NOT NULL,
`date_left` text NOT NULL,
`total_xp` int(15) NOT NULL,
PRIMARY KEY (`team_member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
There's several ways to write this query.
To me this is the easiest to read and understand:
select * from members where member_id not in (select member_id from team_members).
This is a really simple way to write it. If you decide you want everything you can quickly comment out the where clause:
select m.* from members m left outer join team_members tm on m.member_id = tm.member_id
where tm.member_id is null
This way doesn't seem very popular from the SQL I read but I think it's straightforward:
select m.* from members m where not exists
(select member_id from team_members tm where tm.member_id = m.member_id)
On the face of it, the below query is fine
SELECT members.member_id
FROM members
LEFT OUTER JOIN team_members
ON team_members.member_id = members.member_id
WHERE team_members.member_id IS NULL
This will do, but on reading your question again, you seem to have a date_left column and if you want only those members who have not yet left a team then
SELECT members.member_id
FROM members
LEFT OUTER JOIN (SELECT *
FROM team_members
WHERE team_members.date_left != '') CURRENT_TEAMS
ON CURRENT_TEAMS.member_id = members.member_id
WHERE CURRENT_TEAMS.member_id IS NULL
SQLFiddle example
http://www.sqlfiddle.com/#!2/46b25/6/0