using distinct on single table column - mysql

I am selecting data from 3 tables [2 tables with a relational table in between them] The below image is a screenshot of the result
Is there a way in which i can apply DISTINCT on certain columns such that i can get the results as below? NB// i have edited the below image
Below is my sql code

No. You can't apply distinct on single column unless, you are pulling just one column from table.
Distinct applies on complete row. It will get you unique rows in table

Related

Dynamically Pivoting Table Data | mySQL

Essentially I have a table in my database called Table1 with the following data:
The table has a ProductID that repeats because the values of AssignedColour, ColourFinding and ColourPower vary.
I would like to present all ProductID data in one single row, meaning if there is more than one AssignedColour, ColourFinding and ColourPower listed, it will contain a number at the end.
The final result I of the SELECT query should look like the following:
The number of columns presented horizontally is based on the number of AssignedColour per ProductID
Is something like this possible to accomplish in a mySQL SELECT Query?
An SQL query cannot expand the number of columns of the result set depending on the data values it discovers during query execution. The columns in the SELECT-list must be fixed at the time the query is prepared, before it reads any data.
Also the column names cannot be changed during the query execution. They must be set at the time the query is prepared.
There's no way to do what you are describing in a single SQL query. Your options are:
Do two queries: one to enumerate the colors per product, and then use the result of the first to format a second query with the columns you want.
Do one query to fetch the data in rows as it exists in your table, then write code in your app to display it in rows however you think is best.
Either way, you have to write at least a bit of code in the client. You can't do this in one query.

How can I control table name when multiple tables are selected?

I'm using MySQL and I selected three tables in one statement like that:
SELECT * FROM tb_i, tb_s, tb_t
But I need case control by their table names. If a row from tb_s it returns tb_s on additional column table_name.
How can I get table name from this statement?
The way you add up your tables now is a JOIN - meaning that every row is a Cartesian product of three of them - so each row is returned from all the three tables.
If you would like to concatenate the data from the three tables you should use UNION ALL which assumes that the columns are of the same structure. Then you would able to mark the origin table, with an addition constant field.
For instance in your case:
SELECT tb_i.*,'tb_i' as source
FROM tb_i
UNION ALL
SELECT tb_s.*,'tb_s'
FROM tb_s
UNION ALL
SELECT tb_t.*,'tb_t'
FROM tb_t
where the source column is a constant string per each table.

Selecting multiple rows at once when I know what ID's I want

I have a table with 2 columns...ID and Content.
Is there a way to select data from multiple rows at once from a mysql database when I know what row ID's I want to retrieve data from.
Possibly putting the Content column results into an array or some sort if that is the best way. I am trying to prevent a ton of sql query's. There are just 2 columns, an ID and a content column.
Any input would be great.
You can use an IN statement to achieve this:
SELECT *
FROM mytable
WHERE ID IN (1,2,3,4)

MS Access SQL Unequal join for 3 or more tables

I'm thinking of switching to using temp tables and vba.
I want to do this. I have multiple tables, in these tables may or may not have fields with items that have a one to many or one to one relationship. I know what those relationships are (and will create multiple queries accordingly). What I'm hunting for is each value that DOES NOT EXIST in every other table. To make an example:
Say we have 3 single column tables, table 1 is {x, y, z}, table 2 is {a, x, z}, and table 3 is {a,b,x,y,z}, the result will be b for t3 (yes I need to know where the error is). Pretty much, I want to use the unequal wizard but for 3 or more tables.
I may want to look for any item that exists in some but not all other tables. If you want to speak on that, it would be helpful, but I think that is strictly in the vba realm.
I think the challenge here is the open-endedness of the problem you are trying to solve. Varying column names, table names, and uniqueness thresholds across all tables would make it a bit more difficult. In the way I show below, I don't think it would be the most efficient, query-wise, but would be relatively easy to script. The following code assumes values in the tables are unique within each table.
There are 3 queries total:
qry_001_TableValues_ALL
SELECT Table1.MyValue, "Table1" AS Source
FROM Table1
UNION
SELECT Table2.MyValue, "Table2" AS Source
FROM Table2
UNION SELECT Table3.MyValue, "Table3" AS Source
FROM Table3;
qry_002_TableValues_Unique:
SELECT qry_001_TableValues_ALL.MyValue
FROM qry_001_TableValues_ALL
GROUP BY qry_001_TableValues_ALL.MyValue
HAVING (((Count(qry_001_TableValues_ALL.MyValue))=1));
qry_003_TableValues_UniqueWithSource:
SELECT qry_002_TableValues_Unique.MyValue, qry_001_TableValues_ALL.Source
FROM qry_002_TableValues_Unique INNER JOIN qry_001_TableValues_ALL
ON qry_002_TableValues_Unique.MyValue = qry_001_TableValues_ALL.MyValue;
The first table is the one you would need to script out if columns\tables changed. It is looking across all tables and creating a unique list of values from the specified field. The second query looks to look up the Source table name against the original unique value query for all values which have a count of 1, post aggregation. This means of all tables involved, there is only one instace of the values returned, and it joins against the original unique value list again to determine what the source table is. You can script a change to the HAVING clause here to see if there are x tables which contain the value. The final query is simply the one you run to give you the final report of the values you are looking for and where they reside.
Hope this is in the ballpark of what you are trying to do.

How would I display two separate tables within the same query/report without combining each entry?

As of right now, creating a query with all records from both tables I want to display gives me every record for table b for the first record of table a, then every record of table b for the second record of table a, and so on.
SELECT *
FROM tblSales, tblRepair;
I want to be able to format these tables so that records from each table are displayed within a report, but separately (not joined). Both these tables contain sales data that need to be displayed and calculated together on a daily basis, but my problem right now is getting the data out of these tables and together in a format that doesn't join each record together.
Thanks in advance.
You can use a UNION query to combine both tables. I've added a dummy column to distinguish between the two tables:
SELECT *,'Sales' AS TheTable FROM tblSales
UNION ALL SELECT *, 'Repairs' FROM tblRepairs;
This will list all the Sales records first, followed by all the Repairs. You can add an ORDER BY clause to change this.
Alternatively, depending on the type of report you are creating, you could base the main report on one table and add a subreport based on the second.