I have two tables with columns as follows:
khs (id, year, program, codemk)
subjects (id, year, program, codemk,namemk, tm, pr, lp)
I want to have a result containing:
codemk, namemk, tm, pr, lp
Note: Fields year, program and codemk in khs table each is not unique also in subjects table. But combine the 3 value of those fields make a unique value.
I tried this:
SELECT khs.id khs.codemk, subjects.namemk, subjects.tm, subject.pr, subjects.lp
FROM khs
RIGHT JOIN subjects
ON khs.year + khs.program +khs.codemk = subjects.year + subjects.program + subjects.codemk;
but the result for tm, pr and lp is not what I expected. What am I missing? Sorry I am new to MySQL and how do i create it to new table view.
Thanks in advance.
Maybe this:
SELECT khs.id khs.codemk, subjects.namemk, subjects.tm, subject.pr, subjects.lp
FROM khs
RIGHT JOIN subjects
ON khs.year=subjects.year, khs.program =subjects.program, khs.codemk = subjects.codemk;
But not sure if that is what you asked, otherwise:
SELECT khs.id khs.codemk, subjects.namemk, subjects.tm, subject.pr, subjects.lp
FROM khs
RIGHT JOIN subjects
ON CONCAT(khs.year,khs.program, khs.codemk) = CONCAT(subjects.year, subjects.program, subjects.codemk);
Why use a join? Why not try something like this:
SELECT codemk,namemk,tm,pr,lp
FROM khs,subjects
WHERE khs.year = subjects.year
I am not sure what is your result expectetions (could you provide it please? as well as samples of data tables) but you can try for now:
SELECT khs.id khs.codemk, subjects.namemk, subjects.tm, subject.pr, subjects.lp
FROM khs
INNER JOIN subjects
ON khs.year = subjects.year
AND khs.program = subjects.program
AND khs.codemk = subjects.codemk;
Related
I have an annoying problem that I cannot get past.
I have a photograph database broken up into two tables:
Table 1 (snaps) consists of four columns:
'photoid', 'filename', 'location', 'created'
Table 2 (befter) which displays the photos in pairs consists of
'ppairid', 'beforeid', 'afterid', 'description'
Table 2 displays the photo pairs, as beforeid and afterid use the unique photoid INT from table 1. All quite simple it would seem.
BUT one of the queries that I have come up with (by location) duplicates if the location of beforeid and afterid are the same. So for example:-
SELECT *
FROM befter, snaps
WHERE (snaps.photoid = befter.beforeid OR snaps.photoid = befter.afterid)
AND snaps.location = 'oxford'
is fine when the photo location is different but not if they're the same. I've tried adding DISTINCT etc but I can't figure it out.
Any ideas please?
P
Use two left joins:
select b.*, sb.*, sa.*
from befter b left join
snaps sb
on sb.photoid = b.beforeid and sb.location = 'oxford' left join
snaps sa
on sa.photoid = b.afterid and sa.location = 'oxford'
where sb.photoid is not null or sa.photoid is not null;
This includes both the before and after snaps in the result set.
You have to tell the SQL statement what you are fetching from the given tables. If you take an * on two tables, then you will get duplicate results.
Try this:
SELECT
snaps.photoid, snaps.filename, snaps.location, snaps.created, befter.ppairid,
befter.beforeid, befter.afterid, befter.description
FROM
befter
INNER JOIN
snaps
ON
snaps.photoid = befter.ppairid
WHERE
snaps.location='oxford'
AND
(snaps.photoid = befter.beforeid OR snaps.photoid = befter.afterid)
Here I want to access two table field but I cant get success. Here is my little code. please check that. I want to access Analysis.Right and tabl3.right.
I am printing its with foreach loop. like $res['Right'] of Analysis and $res['right'] of tabl3. when I try to print this it's show me error
Undefind index Right.
any one can help me
$qry = "select Analysis.Q_Id, tabl3.Q_Id, Analysis.Right, tabl3.right from tabl3 INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
please help..
you have tow column with right name related to different table so there is not a column name right but 'Analysis.Right ' or 'tabl3.right'
or you can assign an alias for set the column name equalt to Right where you need .. eg:
$qry = "select
Analysis.Q_Id
, tabl3.Q_Id
, Analysis.Right as Right
, tabl3.right as Right_t3
from tabl3
INNER JOIN Analysis ON Analysis.Q_Id = tabl3.Q_Id where Analysis.Q_Id = 3";
Your result set has columns with the same name. Give them different names:
select t3.Q_Id, a.Right as a_right, t3.right as t3_right
from tabl3 t3 inner join
Analysis a
on a.Q_Id = t3.Q_Id
where a.Q_Id = 3;
When you look for the names in your code, look for a_right and t3_right.
Note that you don't need to return Q_Id twice. The ON clause guarantees that the values are the same.
I have two tables:
sk_accounts //details of user
acnt_user_id
acnt_fname //first name
acnt_lname
acnt_profile_picture
acnt_member_class
so on........
sk_following //table containing details of users who are following others
id
flwing_follower_id //id of the user who are followed by other followers
flwing_following_user_id
following_date
I want to display details of the follower based on the following Mysql code.Unfortunately it returns zero rows eventhough there are 3 rows.My query is like this:
$query_following = "SELECT sk_following.flwing_following_user_id,
sk_accounts.acnt_fname,
sk_accounts.acnt_lname,
sk_accounts.acnt_member_class,
sk_accounts.acnt_profile_picture
FROM sk_following
INNER JOIN sk_accounts
WHERE sk_following.flwing_follower_id='$id' AND sk_accounts.acnt_user_id=sk_following.flwing_following_user_id AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'";
$result_following = mysql_query($query_following);
$count_following = mysql_num_rows($result_following);
echo $count_following;
Note:$id and $name contain values
Kindly help me.Thanks in advance.
Try this,
"SELECT sk_following.flwing_following_user_id,
sk_accounts.acnt_fname,
sk_accounts.acnt_lname,
sk_accounts.acnt_member_class,
sk_accounts.acnt_profile_picture
FROM sk_following
LEFT JOIN sk_accounts ON sk_accounts.acnt_user_id=sk_following.flwing_following_user_id
WHERE sk_following.flwing_follower_id='$id'
AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'";
may this help you.
Hard to completely understand without seeing sample data and desired output, but should your JOIN be on the flwing_follower_id and not the flwing_following_user_id?
SELECT sk_following.flwing_following_user_id,
sk_accounts.acnt_fname,
sk_accounts.acnt_lname,
sk_accounts.acnt_member_class,
sk_accounts.acnt_profile_picture
FROM sk_following
INNER JOIN sk_accounts ON sk_accounts.acnt_user_id=sk_following.flwing_follower_id
WHERE sk_following.flwing_follower_id='$id'
AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'
Good luck.
Ok, here's a database.
http://i.stack.imgur.com/j05AB.png
Say I've inserted values into the database for each of these tables, although the IDs would be auto incrementing. There are many BVALUES from each AVALUE, thus the AB table. I have all the AVALUEs from TABLE A in a drop-down list. A user selects an AVALUE which I put into a variable using
$AVALUE = $_POST['AVALUE']
Then I do an sql statement to get the AVALUEs from TABLE A that equal $AVALUE.
$sql = "SELECT AVALUE FROM TABLEA WHERE" . $AVALUE . " = AVALUE";
How do I then get the NAMEID from TABLEA for that AVALUE, then reference to AB where TABLEANAMEID = NAMEID from TABLEA? Then I want to get all the BVALUES by getting all the TABLEBNAMEIDs that correspond to the TABLEANAMEIDs.
I then want those BVALUES in a drop-down list on a seperate HTML page. After a bit of Googling the solution I think would be to do some sort of a loop putting the BVALUES into a variable as all the NAMEIDs from TABLE B increment where the variable would be in an $BVALUE loop and the list values would show with all the BVALUES.
I hope I explained that right. I think I know what I'm trying to do but I have no idea how to actually implement it. Please help guys.
You need to join those table together. What you are describing is an m:n relation. In this case you have do use 2 joins like this:
SELECT * FROM TableA AS a WHERE a.avalue = $AVALUE
JOIN TableAB AS a2b ON a.namevalue = a2b.id_a
JOIN TableB AS b ON a2b.id_b = b.id
Maybe u means, u want to get all BVALUE which has relation with selected AVALUE in table AB
$sql = "SELECT B.NAMEID as id, BVALUE as value FROM TABLEA A
LEFT JOIN AB ON TABLEANAMEID=A.NAMEID
LEFT JOIN TABLEB B ON TABLEBNAMEID=B.NAMEID
WHERE AVALUE = $AVALUE";`
get mysql result
$result = mysql_query($sql);
iterate
echo "<select>";
foreach ($r = mysql_fetch_object($result)) {
echo '<option value="'.$r->id.'">'.$r->value.'</option>';
}
echo "</select>";
I have an issue getting data from three tables, which I want to return using one query.
I've done this before using a query something like this one:
$query = mysql_query("SELECT
maintable.`id`,
maintable.`somedata`,
maintable.`subtable1_id`,
subtable1.`somedata`,
subtable1.`subtable2_id`,
subtable2.`somedata`
FROM
`maintable` maintable,
`subtable1` subtable1,
`subtable2` subtable2
WHERE
maintable.`somedata` = 'some_search' AND
subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id`
")or die(mysql_error());
The problem this time is that the extra details might not actually apply. I need to return all results that match some_search in maintable, even if there is no subtable1_id specified.
I need something that will go along the lines of
WHERE
maintable.`somedata` = 'some_search'
AND IF maintable.`subtable1_id` IS NOT NULL (
WHERE subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id`
)
As you will probably guess, I am not an advanced mysql user! Try as I might, I cannot get the syntax right, and I have had no luck searching for solutions on the web.
Any help much appreciated!
It seems like the basic distinction you're looking for is between an INNER JOIN and a LEFT JOIN in MySQL.
An INNER JOIN will require a reference between the two tables. There must be a match on both sides for the row to be returned.
A LEFT JOIN will return matches in both rows, like an INNER, but it will also returns rows from your primary table even if no rows match in your secondary tables -- their fields will be NULL.
You can find example syntax in the docs.
If I got this right, you need to use MySQL LEFT JOIN. Try this:
SELECT
m.id,
m.somedata,
m.subtable1_id,
s1.somedata,
s1.subtable2_id,
s2.somedata
FROM
maintable m
LEFT JOIN
subtable1 s1
ON
m.subtable1_id = s1.id
LEFT JOIN
subtable2 s2
ON
s1.subtable2_id = s2.id
WHERE
m.somedata = 'some search'
This will return the data of maintable even if there's no equivalent data in subtable1 or 2 OR data of maintable and subtable1 if there's no equivalent data in subtable2.
How about this:
WHERE
maintable.`somedata` = 'some_search'
AND (maintable.`subtable1_id` IS NULL OR (
subtable1.`id` = maintable.`subtable1_id` AND
subtable2.`id` = subtable1.`subtable2_id` )
)
Keep in mind that this will result in a cross product of subtable1 and subtable2 with maintable when subtable1_id is NULL.