inner join with group by function - mysql

I am trying to get results matching a variable using a LIKE operator combined with an INNER JOIN and a GROUPING modifier.
The problem I am having with this query:
-the join will not work with the modifier as it is being grouped by the same column that is being used to join the two tables. I get the error '#1052 - Column 'Agency' in field list is ambiguous'
SELECT Agency,Acronym,last,sum(last),current,sum(current),source,url
FROM `budget_table` INNER join budget_table2 on
budget_table.agency=budget_table2.agency WHERE (Agency) LIKE('%$agency%') GROUP BY Agency
I have looked through other answers but am not able to apply what is posted to what I am doing. Assistance would be appreciated. Is there something wrong with my JOIN?
I am trying to select only agencies common to both tables and then match data against the variable from within those results.

If you have the same column name in two tables, you have to reference the table do you want to get the column value. Write the table name before the column name in your select, where and group.
Example:
SELECT budget_table.Agency /* ...your code... */
WHERE budget_table.Agency LIKE ('%$agency%')
GROUP BY budget_table.Agency

Related

Create table from joined table in MySQL

I have no problem joining the tables, but when I go to create a new table using the joined tables, I get an error saying that I have duplicate columns.
My code:
SELECT *
FROM field
INNER JOIN race
ON field.raceID = race.raceID;
Updated code:
CREATE TABLE fieldrace AS
SELECT f.*, r.*
FROM field f
INNER JOIN race r
ON f.raceID = r.raceID;
That's true of any select. If there are duplicated column names, you have to reference them somehow. For a .* query this would work:
SELECT f.*, r.*
FROM field f
INNER JOIN race r
ON f.raceID = r.raceID;
Individually you can also add aliases. Maybe you have an id column in both race and field tables.
SELECT f.id as field_id, r.id as race_id, ....
FROM field f
INNER JOIN race r
ON f.raceID = r.raceID;
In the query
CREATE TABLE fieldrace AS
SELECT f.*, r.*
FROM field f
INNER JOIN race r
ON f.raceID = r.raceID;
SELECT part produces two columns with the same name in the output.
Two columns with the same name presence is not allowed in table's structure, and the whole query will fail.
General solution is to list each output column in the SELECT part separately with assigning them unique aliases.
If raceID column which is used for joining is the only column whose name interferes then you may use either USING clause instead of ON clause or NATURAL JOIN instead of INNER JOIN.
CREATE TABLE fieldrace AS
SELECT f.*, r.*
FROM field f
INNER JOIN race r USING (raceID);
-- or
CREATE TABLE fieldrace AS
SELECT f.*, r.*
FROM field f
NATURAL INNER JOIN race r;
In both cases the interfered columns will be collapsed into one column which will be placed to the top of created table structure.
Of course when raceID is not the only column whose name interferes then 1st of these queries will fail due to another column duplication whereas 2nd query will use all interfered columns for joining.
You may specify complete or partial structure of newly create table. In this case the amount and relative posession of the columns in the created table won't be changed (will match SELECT output) but all another properties of the columns (datatype, nullability, etc.) and additional objects (indices, constraints, etc.) listed in the structure will be applied. The columns which are absent in the output (including generated ones) will be added into the structure with default values as the most first ones, before the columns used in USING or during NATURAL JOIN even. DEMO.
you can create "view" or name a subquery using "with"
in both cases, you can access it from anywhere in your main query

SQL: How do add a prefix to column names in a JOIN?

I have sql query like this
SELECT * FROM phlegm WHERE JOIN mucus ON phlegm.id = mucus.id JOIN snot ON phlegm.id = snot.id
The problem is those tables contain several columns with identical names.
For example all 3 tables contain the column named test
If I retrieve the result of the query in PHP, then I will only get one value named test ($query->get_result()->fetch_object()->test;), because the other two get overwritten.
Is there some way to edit that query so that it adds a prefix to all columns from a table? For example, column test from table mucus would be referenced in the query as mucus_test and column test from phlegm would be phlegm_test.
One way would be doing
SELECT phlegm.test as phlegm_test, mucus.test as mucus_test FROM phlegm...
But I have a LOT of columns and tables and it would make the query longer than the Great Wall of China if I had to name each field one by one.
So is there some way to add the prefix en masse?
SELECT *, phlegm.test as phlegm_test, mucus.test as mucus_test FROM phlegm...
Used aliasing to retrieve all values associated from all three tables. if you want to reference only specific column do so by using the alias_name.column_name instead of p.*, where * means all columns belonging to table that the alias is associated with( ie. p refers to phlegm).
SELECT p.*, m.*, s.*
FROM phlegm p
JOIN mucus m ON p.id = m.id
JOIN snot s ON p.id = s.id;
I removed the WHERE from your original query above, not sure why it was there.

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?

SQL: Trouble returning distinct like values from a subquery and join

query:
select DOG_CD,ANIMAL_CD
from FKAGM
where FKAG = 12024
Displays 3 rows
DOG_CD - ANIMAL_CD are column names and below each column is 3 numerical values yielded from the above query & I have no clue how to draw a table on here to depict that. =(
There is a table called Dog_Animal that has a column called "Name" (Dog_Animal.Name) that I want to join with this above query. I want to join on ANIMAL_CD as FKAGM table and the DOG_ANIMAL table have the ANIMAL_CD column in common. I'd like to display "Name" right next to the ANIMAL_CD column. The issue is when I join the tables it displays like every instance of DOG_CD and ANIMAL_CD within the Dog_Animal table (Which is thousands) something similar to the below illustration. since the Dog_Animal table has thousands of DOG_CD and ANIMAL_CODE fields populated. I just want it to be limited to the three rows that are returned by the above query (Limit or distinct or something) and input the Dog_Animal.Name next to Animal_CD. I've been working on this for an hour and I know it must be so simple, but I can't seem to get it to work. I am not sure if a subquery is needed or exists, or case, or what. If you can figure this out I would be so thankful!
DOG_CD - ANIMAL_CD - Dog_Animal.Name with the same 3 rows of data just now a name included
select
f.DOG_CD,f.ANIMAL_CD,d.name
from FKAGM f
inner join dog_animal d
on d.animal_cd = f.animal_cd
where f.FKAG = 12024;
inner join would work for you. If you only need to limit it to 3 results, add limit 3 order by f.dog_cd at the end of your query.

select from a table where the dates are different to tha dates in other table

I have some dates in a table and i need a query that the dates in the table are different to the second table, i am intenting with this:
http://sqlfiddle.com/#!2/a1308/2
But it now works
I need for example in the first table:
3202024834
3108984111
3118545645
4608389
2631993
9876534
3114568970
and in other
3202024834
3108984111
3118545645
Then the output of the query is:
4608389
2631993
9876534
3114568970
I am interpreting your question as "Get values in a that are not in b". If so, a good way to approach this is with a left outer join along with a where clause:
select a.msisdn
from msisdn a left outer join
insertados b
on a.msisdn = b.numero
where b.numero is null;
The data is the SQLFiddle doesn't exactly match the data in the question, so the results are a bit different from what is in the question.