I am joining two large tables in MySQL based on a unique identifier they both share. Because there are a large number of fields, I do not want to list out all fields after SELECT. Instead I want to select all fields, but I do not want recurring fields (the shared unique identifier in this case) to be repeated.
With this example query:
SELECT *
FROM Gr3_PracMath_Jan11_D1 as a, student_list_011811 as b
WHERE a.StudentID = b.StudentID
The field StudentID is repeated. Is there a way to prevent this?
I believe that if you do an explicit join with the USING keyword, you won't get a duplicate.
SELECT *
FROM Gr3_PracMath_Jan11_D1
LEFT JOIN student_list_011811
USING (StudentID)
I don't think there is. You might cut your work by listing only half the fields:
SELECT a.*, b.Field1, b.Field2...
It is bad practice to not list out all of the columns, even if there are a lot of them. Just bite the bullet and write them out.
Related
I have two tables, I've been trying to print the result from each but they are being duplicated. These are the two MySQL tables and the result. Notice the duplication.
The sql code for the project is:
SELECT * FROM savings,savtype WHERE cust_id=".$_SESSION['user']
I'm also looking for a work around this, in the meantime, id appreciate any assistance on this.
because you are not specifying how the two tables are related. You need to add that, either via an explicit ... JOIN ... (USING|ON)
SELECT
*
FROM
savings JOIN savtype USING (savtype_id)
WHERE
cust_id = ".$_SESSION['user']
or by providing the criteria in the where clause.
SELECT
*
FROM
savings, savtype
WHERE
savings.savtype_id = savtype.savtype_id AND
cust_id = ".$_SESSION['user']
As I understand from the screenshot you added, it makes joint between those tables, and what you probably want it left join from savings and savtype tables.
SELECT *
FROM `savings`
LEFT JOIN `savtype`
ON savings.savtype_id=savtype.savtype_id
where cust_id=".$_SESSION['user'] .";
Update if this did the trick,
You can learn more about left join here: https://www.w3schools.com/sql/sql_join_left.asp
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?
I'm trying to perform a join operation on two tables linked by item IDs. However, the problem with them is that they've got columns with the same name as follows:
items
(ID, **Quantity**, etc. /*[nothing in etc. is shared by status' columns]*/)
status
(ID, **Quantity**, etc. /*[nothing in etc. is shared by items' columns]*/)
I want to get all records from these tables and join them, but I don't know what the SQL query would look like. I know it'd be something like:
SELECT *
FROM items
LEFT OUTER JOIN status
ON items.ID = status.ID
and I know I need aliases for the two quantity columns (which I know how to do), but where does the latter part of the query fit in?
In general, I recommend avoiding SELECT * queries. Just select the specific columns you need, and if there are duplicate column names you can easily assign aliases for them.
SELECT i.col1, i.col2, i.quantity AS item_quantity, s.col3, s.col4, s.quantity AS status_quantity
FROM items AS i
JOIN status AS s ON i.ID = s.ID
But if you really need to select all columns, you can use the solution in Marc B's answer.
Running an INNER JOIN type of query, i get duplicate column names, which can pose a problem. This has been covered here extensively and i was able to find the solution to this problem, asides from it being fairly logical, by SELECTing only the columns i need.
However, i would like to know how i could run such a query without actually returning any of the columns from the joined table.
This is my MySQL query
SELECT * FROM product z
INNER JOIN crosslink__productXmanufacturer a
ON z.id = a.productId
WHERE
(z.title LIKE "%search_term%" OR z.search_keywords LIKE "%search_term%")
AND
z.availability = 1
AND
a.manufacturerId IN (22,23,24)
Question
How would i modify this MySQL query in order to return only columns from product and none of the columns from crosslink__productXmanufacturer?
Add the table name to the *. Replace
SELECT * FROM product z
with
SELECT z.* FROM product z
Often when you are doing this, the intention may be clearer using in or exists rather than a join. The join is being used for filtering, so putting the condition in the where clause makes sense:
SELECT p.*
FROM product p
WHERE (p.title LIKE '%search_term%' OR p.search_keywords LIKE '%search_term%') AND
p.availability = 1 AND
exists (SELECT 1
FROM pXm
WHERE pXm.productId = p.id AND pxm.manufacturerId IN (22, 23, 24)
);
With the proper indexes, this should run at least as fast as the join version (the index is crosslink__productXmanufacturer(productId, manufacturerId). In addition, you don't have to worry about returning duplicate records, if there are multiple matches in crosslink__productXmanufacturer.
You may notice two other small changes I made to the query. First, the table aliases are abbreviates for the table names, making the logic easier to follow. Second, the string constants use single quotes (the ANSI standard) rather than double quotes. Using single quotes only for string and date constants helps prevent inadvertent syntax errors.
I've a table with fields id_osztaly, id_csoportositas and name (and some other fields but there aren't important).
I want create a query with the follow result: I want combine the name fields. I can't explain so I show an example:
the datas in the table (id_osztaly, id_csoportositas and name order):
1,1,Group1A
1,1,Group1B
1,2,Group2A
1,2,Group2B
I want the follow combine from name:
Group1A-Group2A
Group1A-Group2B
Group1B-Group2A
Group1B-Group2B
(similar the permutation). I can do this with a JOIN, it's ok. But when I three different value of id_csoportositas:
1,1,Group1A
1,1,Group1B
1,2,Group2A
1,2,Group2B
1,3,Group3A
1,3,Group3B
1,3,Group3C
I want:
Group1A-Group2A-Group3A
Group1A-Group2A-Group3B
Group1A-Group2A-Group3C
Group1A-Group2B-Group3A
Group1A-Group2B-Group3B
Group1A-Group2B-Group3C
Group1B-Group2A-Group3A
Group1B-Group2A-Group3B
Group1B-Group2A-Group3C
Group1B-Group2B-Group3A
Group1B-Group2B-Group3B
Group1B-Group2B-Group3C
Yes, it's double join. But I don't know how many different id_csoportositas exist in table. First blick I think I need same number of JOIN as the number of different id_csoportositas.
Is there any trick in (my)sql to do this or should I do it in PHP with a for-cycle?
EDIT maybe I wasn't clear. I know how can I JOIN same table two times or three times. If I've two different id_csoportositas I need only one JOIN. If I've three different id_csoportositas I need two JOIN - I can do this too. But I don't know how many different id_csoportositas exist so I don't know how many JOIN will need. The number of JOINs depends on number of different id_csoportositas and I don't know the number of id_csoportositas without a query.
And I want to group by id_osztaly and different id_osztaly has different id_csoportositas.
I hope it's clear now.
Won't something like this work:
SELECT * FROM
(
(SELECT * FROM table WHERE something = 1) a,
(SELECT * FROM table WHERE something = 2) b,
(SELECT * FROM table WHERE something = 3) c
)
You just select like this and it would provide all possible variations of the joins :)