Issue with Join between Query in PHPmyadmin and website - mysql

I'm trying to do I think a simple left join between Table A and Table B. Table A contains a master identifier A and the identifier for table B. When I run the query in PHPmyadmin I get the expected results: Identifier A and Identifier B (or NULL for Identifier B when there is no record in the table B) when run on the website the results either give me both identifiers if present in both tables or neither identifiers if only found in Table A.
Can anyone give me an idea why on the website I don't get Identifier A and then NULL for identifier B?
Query
"SELECT jobindex.*, jobrequest.*, somebody.Name
FROM jobindex
LEFT JOIN jobrequest ON jobindex.idJobIndex=jobrequest.idJobIndex
JOIN somebody ON jobindex.idSomebody=somebody.idSomebody
WHERE jobindex.JobStatus NOT IN ('Closed', 'Removed') "

If you're returning the data as an associative array (in PHP), dictionary (in Python), or hash (in Perl), you'll have a problem if two tables have columns with the same name. The key just contains the column name, without the table prefix, so you can't get the column from both tables. It will pick one of the tables to use, probably the last one in the SELECT clause.
You should give the columns aliases so that you can distinguish them:
SELECT jobindex.idJobIndex AS ji_id, jobrequest.idJobIndex AS jr_id, jobindex.*, jobrequest.*, somebody.Name
Now when there's no match, $row['ji_id'] will contain the value from jobindex, $row['jr_index'] will be NULL.

Based on the suggested answer I changed the line
SELECT jobindex.*, jobrequest.*, somebody.Name
to a full select statement with each field explicitly stated
SELECT JI.idJobIndex,JI.idSomebody,JI.JobType, JI.JobReceivedStaff, JI.JobTitle, JI.JobStatus ..... JR.idJobRequest, JR.JobResponseStaff, somebody.Name
FROM jobindex as JI
This has corrected the issues I was having.

Related

Combine table data in MySQL using JOIN

I'm trying to join two tables in MySQL, in one I have a set of IDs (of the type GTEX-14BMU-1526-SM-5TDE6) and a set of type's of tissue(SMTS), I have to select the IDs for the tissue type 'Blood' (which is another column of the same table), and then I have to take only the first two strings (GTEX-14BMU) from the ID name and make a list of the different ones.
Then I have to compare this to a second table in which I have a list of IDs that already are in the type (GTEX-14BMU) which have to meet the condition that the column sex of this same table is 2.
The expected result is a list with the IDs which are sex type 2 and have tissue type 'Blood', meaning the ones that are coinciding. I'm trying to solve this by using JOIN and all the needed conditions in the same statement, which is:
mysql> SELECT DISTINCT SUBSTRING_INDEX(g.SAMPID,'-',2) AS sampid, m.SUBJID, g.SMTS, m.SEX
-> FROM GTEX_Sample AS g
-> JOIN GTEX_Pheno AS m ON sampid=m.SUBJID
-> WHERE m.SEX=2
-> AND g.SMTS='Blood';
But I'm either getting too many results from the combination of all possibilities or I'm getting an empty set. Is there any other way to do this?
Here:
JOIN GTEX_Pheno AS m ON sampid=m.SUBJID
I suspect that your intent is to refer to the substring_index() expression that is defined in the select clause (which is aliased sampid as well). In SQL, you can't reuse an alias defined in the select clause in the same scope (with a few exceptions, such as the ORDER BY clause, or the GROUP BY clause in MySQL). So the database thinks you are referring to column sampid of the sample table. If you had given a different alias (say sampid_short) and tried to use in the ON clause of the join, you would have met a compilation error.
You need to either repeat the expression, or use a subquery:
select substring_index(g.sampid, '-', 2) as sampid, m.subjid, g.smts, m.sex
from gtex_sample as g
inner join gtex_pheno as m on substring_index(g.sampid, '-', 2) = m.subjid
where m.sex = 2 and g.smts = 'blood';

Fetching value from table and passing it in url

How can i use in table field values in the url
SQL Query wherein all 3 tables are joined
select * from nfojm_usedcar_variants cv
inner join nfojm_usedcar_products cp
inner join nfojm_usedcar_categories cc on
cc.id=cp.prod_cat_id and
cp.id=cv.v_prod_id and
cv.state='1' order by cv.id desc
Output as checked
Then it combines all 3 tables
nfojm_usedcar_variants
nfojm_usedcar_products
nfojm_usedcar_categories
However - all 3 tables have unique field i.e id (but with different values)
I need to pass on value of id and v_prod_id in a url
say url been :-
<a href="index.php?option=com_usedcar&pid='.$row->v_prod_id.'&vid='.$row->id.'">
But id been common field in most of the tables hence its not picking in correctly from nfojm_usedcar_variants,
Can some one help to modify a function so as to fetch in value of id and v_prod_id from the respective table of nfojm_usedcar_variants
thanks
If you have multiple tables in a join that share a common column name, and you need them, then alias them. Such as:
select a.id as aid,a.theName,b.id as bid,b.year
from tableA a
join tableB b
on b.id=a.id
then refer to those columns as aid and bid in your code that follows.
Try to avoid Ever doing a select *. Be explicit. You never know what comes flying out of a select * typically. And odds are you don't need it all. Select * is fine for messing around, but not for production code. And you can't control common column names with select *. We like to control things afterall, no?

How to search data from multiple tables using Mysql?

I have three tables :
bldr_prjct,bldr_prjct_attr,bldr_prjct_attr_ref;
i want to get bldr_prjct data where bldr_prjct attributes are stored in bldr_prjct_attr.
bldr_prjct_attr_ref : In this table i have defined values all attributes related to projects :
Screenshots :
bldr_prjct
bldr_prjct_attr_ref :
bldr_prjct_attr :
My query :
SELECT
`p`.`ID`,`p`.`PRJCT_NM`,`p`.`SLUG`,`p`.`STS_CD`,
`p`.`PRJCT_GEO_LT`,`p`.`PRJCT_GEO_LG`
FROM
`bldr_prjct` `p`, `bldr_prjct_attr_ref` `pr`, `bldr_prjct_attr` `pa`
WHERE
`pa`.`REF_ID` IN (SELECT `ID` FROM `bldr_prjct_attr_ref` WHERE `PRNT_ID`=3)
Firstly, don't use implicit JOIN syntax(comma separated) , use the proper syntax of join, that will help you avoid this kind of mistakes.
You query is not working because you are missing the join relations , you may have to adjust it a bit, i guessed bldr_prjct is joined to bldr_prjct_attr_ref by id=prnt_id although you have prnt_id column in this table, so change it if needed.
SELECT `p`.`ID`,`p`.`PRJCT_NM`,`p`.`SLUG`,`p`.`STS_CD`,`p`.`PRJCT_GEO_LT`,`p`.`PRJCT_GEO_LG`
FROM `bldr_prjct` `p`
INNER JOIN `bldr_prjct_attr` `pa`
ON(`p`.id = `pa`.prnt_id )
INNER JOIN `bldr_prjct_attr_ref` `pr`
ON(`pa`.ref_id = `pr`.id and `pr`.prnt_id = 3 )
I might be missing something, but your tables don't appear to have any common reference. For instance, table bldr_prjct_attr_ref does not include any field pointing to the relevant record in table bldr_prjct. If my understanding is correct, you will have to alter these tables to allow some time of cross-referencing. For instance, add to table bldr_prjct_attr_ref a field (column) bldr_prjct_ID that points to the corresponding record in table bldr_prjct.
There is one field in the third table that appears to reference a record in the first, but when you issue a SELECT of three tables and one of them returns an empty result, then the overall select returns empty.

How to force mysql to show schema name along with column names in a query?

I need to join 3 tables which have some columns with same name, like id and some foreign keys columns.
I make a select query and the results come with table names only. How to get results like "dbname"."columnname" in my queries so I can identify from which table is each columns without having to specify every columns in the query (using only an *)?
Note: I use Delphi with ZeosLib, so a solution using these tools would be OK as well. But I prefer to set this in the data base.
You have to create an alias for your field name in your query
SELECT a.ID, b.ID
FROM a
JOIN b
You need doblue quote " for field names with special characters, so change it to.
SELECT a.ID "a.ID", b.ID "b.ID"
OR
SELECT a.ID "MeaningfullName", b.ID "OtherName"
For example here I have two fields name "sent_dt" and change one to previous_time
SQL Fiddle Demo

MySQL - Left joins without duplicate columns

I have an issue with some of the join statements I'm trying to use. I have two tables that need to be joined, with both featuring all of their information. They're as follows.
INSTITUTION
IName | ALocation_ID | IPicture
ADDRESS
ALocation_ID | AStreet | AZip | ...(other relevant fields)
I've been trying to use:
CREATE VIEW InstitutionView
AS SELECT * FROM INSTITUTION
LEFT JOIN ADDRESS
ON INSTITUTION.ALocation_ID=ADDRESS.ALocation_ID;
but the error I receive says something about duplicate columns. What am I doing wrong?
You will have to select the columns individually. Hopefully this helps you out a little.
CREATE VIEW InstitutionView
AS
SELECT address.id,address.iname,address.alocation_id,ipicture,institution.astreet,institution.azip
FROM INSTITUTION
LEFT JOIN ADDRESS
ON INSTITUTION.ALocation_ID=ADDRESS.ALocation_ID;
That is because ALocation_ID column is present in both the tables.
Try creating the view explicitly naming the required columns.
CREATE VIEW InstitutionView
AS SELECT Iname,INSTITUTION.ALocation_ID,IPicture,AStreet,AZip ...
FROM INSTITUTION
LEFT JOIN ADDRESS
ON INSTITUTION.ALocation_ID=ADDRESS.ALocation_ID;
Conceptually, JOIN first creates an intermediate cross-product where the columns are referenced by a table name or alias dotted by a column name from that table; then ON and WHERE filter out rows that don't match to give a second intermediate result. If a column name appears only in one table then you can leave out the table & dot to refer to the column.
MySQL 5.6 Reference Manual :: 13.2.9 SELECT Syntax
You can refer to a column as col_name, tbl_name.col_name, or db_name.tbl_name.col_name. You need not specify a tbl_name or db_name.tbl_name prefix for a column reference unless the reference would be ambiguous. See Section 9.2.1, “Identifier Qualifiers”, for examples of ambiguity that require the more explicit column reference forms.
MySQL 5.6 Reference Manual :: 9.2.1 Identifier Qualifiers
Suppose that tables t1 and t2 each contain a column c, and you retrieve c in a SELECT statement that uses both t1 and t2. In this case, c is ambiguous because it is not unique among the tables used in the statement. You must qualify it with a table name as t1.c or t2.c to indicate which table you mean.
Hence:
CREATE VIEW InstitutionView
AS SELECT IName,I.ALocation_ID,IPicture,AStreet,AZip,...
FROM INSTITUTION I
LEFT JOIN ADDRESS A
ON I.ALocation_ID=A.ALocation_ID;
You might think that if a JOIN is ON or WHERE "=" then there would be no ambiguity. However:
In the case of INNER JOIN, if there were no implicit conversions then columns compared equal would have the same value; but otherwise different values can compare "=". So you can't use just the column name to identify one value.
Moreover for LEFT JOIN, unmatched rows in the left table are extended by NULLs and are added to give a third intermediate result; so in a row a non-NULL in a column in one table can appear with a NULL in the same column of the other table. So again you can't use just the column name to identify one value.
Moreover there doesn't even need to be a test of equality of two columns in a JOIN, or even mention both of or either of columns with a shared name. So the result can have two columns (one from each input table) sharing a name where there is no expectation of equality.