I have a SQL statement to select data:
SELECT DISTINCT count(table.data) as cntd, table.datatwo
FROM table LEFT JOIN tabletwo ON table.id=tabletwo.tableid
WHERE tabletwo.dated='2016-11-17' AND table.datafive='1'
If I keep it the way above - it returns the data as below:
cntd datatwo
4 92
However the correct data should be:
cntd datatwo
2 92
2 93
As there are two different datatwo. When I select the statement without cont - it shows both datatwo.
datatwo
92
93
Where am I making a mistake?
Despite how random and unintuitive your table naming is, it appears what you want is to use a GROUP BY statement:
SELECT
DISTINCT count(table.data) as cntd,
table.datatwo
FROM table
LEFT JOIN tabletwo ON table.id=tabletwo.tableid
WHERE tabletwo.dated='2016-11-17'
AND table.datafive='1'
GROUP BY tabletwo.tableid
The COUNT statement is an aggregate function so it causes the rows to collapse down to one. Grouping by the unique value for each row will cause it to aggregate in groups.
Related
I have two mysql tables. And essentially two queries (one to each table) that I want to combine within a single SQL Query. Seems like it should be easy (kind of like an inner Join) But inner join is DUPLICATING non-uniqe values from table 2 into my results array... I dont want them duplicated... I want those duplicates from table/query 2 combined into the result record of query1
Query 1 Gets records from table 1. Results are unique. (one ID returns one record) It's simply returning all fields on records where an ID is equal to one of my conditions. [Im using IN instead of a bunch of OR's)
$sid = "'M-179','M-242','M-231','Q-2thru5'" ;
$query = "SELECT * FROM table1 WHERE IN ($sid)`
Query 2 gets records from table 2. But results are NOT unique. One ID can return many records.
$query2 = "SELECT extra_data, pub_url FROM table2 WHERE IN ($sid)";
So I want EACH extra_data & pub_url field from ALL returned records just slapped onto the end of the query 1 result. Am I making sense? So the result would look something like this...
[0] => Array
(
* all my returned fields from the
record returned by query 1 here
$row['extra_data']
$row['pub_url'] <-returned record from query 2
$row['extra_data']
$row['pub_url'] <-another returned record from query 2
$row['extra_data']
$row['pub_url'] <-any another returned
record from query 2, etc..
)
What do you mean by combining the result?
Do you want the results from query2 to appear in the same
relevant rows but just extra columns, or you want them to
appear as new rows.
For the first case, you will have to use JOIN.
But I have a feeling that what you want to do is the second
case. In this case, you will have to use UNION. Note, however,
that in this case the columns of the two queries must match. So
the union of two queries would look like this:
-- pseudo code only
SELECT extra_data, pub_url FROM table1 WHERE IN ($sid)
union
SELECT extra_data, pub_url FROM table2 WHERE IN ($sid)
Try something similar to this, which would join the results of table 1 to table 2 on the column containing the sid value.
SELECT a.*, b.extra_data, pub_url
FROM table1 a
left outer join table2 b on a.sid = b.sid
WHERE a.sid IN ($sid)
I have a complicated select like:
select id from table
left join...
left join... (a lot of joins)
where ... (a lot of ANDs and ORs)
order by... (a lot of orders)
and I'm getting a result like:
1234
5565
7212
2212
etc.
I have an id which belongs to the result-set, like 7212, and want to know which row in the result-set matches the id (starting with row 0 this would be row 2 in my example).
Right now I'm reading all data and compare it in php, but I was wondering if there is a SQL-statement which does that for me and results 2 when entering 7212.
In fact I want to get the previous and next ids (row 1 and row 3 = 5565 and 2212), if thats somehow possible in 1 query that would be even better.
You can select the number row:
select #rownum:=#rownum+1 No, foo, bar from table, (SELECT #rownum:=0) r;
It's a possible duplicate of the question asked here: How to show sequential number in MySQL query result
Add an auto_increment index for each selected rows:
SELECT
#i:=#i+1 as index,
id
FROM table, (SELECT #i:= 0) AS i
LEFT JOIN...
LEFT JOIN...(a lot of joins)
WHERE ... (a lot of ANDs and ORs)
ORDER BY... (a lot of orders)
Give you this:
index id
1 1234
2 5565
3 7212
4 2212
I am a beginner regarding SQL queries so I hope that someone can help me with this.
I have a table that has 2 columns that are called MID and jachtID.
What I need is the count of how many of the same jachtIDS there are with different MIDS attached to them like this:
MID jachtID
89 10
95 10
83 11
The result should look something like this:
MID jachtID count
89 10 2
95 10 2
83 11 1
And I need this for all of the rows
I have tried using
SELECT count(DISTINCT jachtID) FROM table
But this just gives me 1 big number and not the result that I need.
Any help would be appreciated.
You can try the following query:
SELECT
T.MID,
T.jachtID,
jT.total
FROM table T INNER JOIN
(
SELECT
jachtID,
COUNT(*) total
FROM table
GROUP BY jachtID
)AS jT
ON T.jachtID = jT.jachtID
First get count of each jachtID by the following query:
SELECT
jachtID,
COUNT(*) total
FROM table
GROUP BY jachtID
Then make an INNER JOIN between the main table and the above query and get the corresponding jatchtID count thereby.
You might be able to do this with some GROUP BY magic, but I'm not sure, since you want all the rows. Using a sub query will work, though.
SELECT
a.MID,
a.jachtID,
(SELECT count(b.jachtID) FROM table AS b WHERE b.jachtID= a.jachtID) AS `count`
FROM table AS a
I've been looking for this but I didn't found what I need. I'm creating an admin page and I need to access the db values from a php interface.
I need to get all the rows from many tables. First I tried with:
SELECT * FROM t1, t2, t3, t4
But that's duplicating rows many times, something odd... The first table have 8 rows, the second 8 too, the third 7 and the fourth 1. There are 24 rows in total, but if I execute that query I get 448 rows... why?
Let's say that I have 4 tables and I want to get all that data in one query, get the columns in the order I put the tables in the query but without duplicate... There are no conditions, just get all the data like if I make 4 queries:
SELECT * FROM t1
SELECT * FROM t2
SELECT * FROM t3
SELECT * FROM t4
If I do this no data is duplicated... why?
How would I merge those 4 queries but still getting the same results? (no duplicated data)
Like I said in the comment; you're executing a cross join. Don't do that!
But in general, if you want to exclude duplicates, you could SELECT DISTINCT ...
I tried the following query and got an error in MySQL. I kind of know why it's throwing me a syntax error (it's related to the two DISTINCT queries being run on the same line) but still believe that its possible (somehow). The two columns card_type and split_type each contain one of five values ("Attack", "Foundation", "Character", "Asset", "Action"). What I would like to do is create a query that would count a record as a +1 if "Attack" appeared in either card_type or split_type.
SELECT DISTINCT(ufs.card_type), DISTINCT(ufs.split_type), COUNT(*) AS COUNT
FROM jg1_products p
LEFT JOIN jg1_product_types pt ON p.products_type = pt.type_id
LEFT JOIN jg1_products_to_categories ptc ON p.products_id = ptc.products_id
LEFT JOIN jg1_cards_ufs ufs ON ufs.products_id = p.products_id
WHERE type_handler LIKE "%product_cards%"
AND ptc.categories_id = 89
GROUP BY ufs.card_type
I guess I should be a BIT clearer in my explanation:
I was hoping that the SQL query would return the total number of results which have met a condition in either Column A or Column B.
EXAMPLE:
If the word "Attack" appeared in either column A or column B, count that as one.
If the word "Attack" appeared in Column A but Column B contains the word "Foundation", That would be +1 to Attack and +1 to Foundation.
In the end, the function/SQL would return to number of times that word (either of the five possibles) would appear between those two columns. So in short I used the "DISTINCT" command (incorrect I'll add) so that all the distinct values in column A are returned and the same for B. If they match, count that as one.
Q: "a query that would count a record as a +1 if "Attack" appeared in either card_type or split_type."
A: It's not clear what resultset you want to return.
EDIT
Based on your comment/clarification, if I'm understanding this correctly, if the rows returned by the SELECT were something like this example:
card_type split_type
--------- -----------
Attack Attack
Attack Foundation
Attack Crescendo
Foundation Foundation
You want a resultset something like this:
Attack 3
Foundation 2
Crescendo 1
You want 3 returned for "Attack", because three rows had the value 'Attack' in either card_type or split_type. That is, you don't want to return a count of 4, the number of times the value appeared.
To get that result, using a COUNT aggregate, I would run this as two separate queries, and combine the results of the two queries using a UNION ALL set operator. The first query would get a count by just card_type, the second query would get a count by split_type. The "trick" would be for the second query to exclude any rows where the split_type matches the card_type.
The two combined queries would be used as an inline view, the outer query would combine the separate counts using a SUM() aggregate function.
I would do the query using a form something like this:
SELECT c.type
, SUM(c.cnt) AS cnt
FROM ( SELECT ufs.card_type AS `type`
, COUNT(1) AS cnt
FROM ...
GROUP BY ufs.card_type
UNION ALL
SELECT ufs.split_type AS `type`
, COUNT(1) AS cnt
FROM ...
AND NOT (ufs.split_type <=> ufs.card_type)
GROUP BY ufs.split_type
) c
GROUP BY c.type
You'd plug in the row source of the original query two times, replacing the ... in the query above.
Previous answer:
Assuming that you have a SELECT that returns the rows you want checked, one "trick" is to use an expression in the SELECT list to perform a conditional test, and return either a zero or one, and then use SUM() aggregate to return a "count" of the records that meet the specification..
SELECT SUM(IF(ufs.card_type LIKE '%Attack%' OR ufs.split_type LIKE '%Attack%',1,0)) AS cnt
FROM jg1_products p
LEFT JOIN jg1_product_types pt ON p.products_type = pt.type_id
LEFT JOIN jg1_products_to_categories ptc ON p.products_id = ptc.products_id
LEFT JOIN jg1_cards_ufs ufs ON ufs.products_id = p.products_id
WHERE type_handler LIKE "%product_cards%"
AND ptc.categories_id = 89
This query returns a single row, unlike your original query that returns multiple rows. (Again, it's not clear what resultset you want returned; if you actually want to return a count for each distinct card_type, which would be returned if we included a GROUP BY ufs.card_type clause.
MySQL also provides a convenient shorthand for the boolean expression: the evaluation of a boolean expression returns 1 if TRUE, 0 if FALSE, and NULL if NULL. So this expression:
SELECT SUM(ufs.card_type LIKE '%Attack%' OR ufs.split_type LIKE '%Attack%')
FROM ...
is equivalent to the expression in the query above, except for the handling of NULL values.
It's not clear whether you want to check if the column "contains" the string 'Attack' as part of the string, or is the entire string; to check if the value of the column is exactly equal to 'Attack', use the equality comparison instead of LIKE
SELECT SUM(ufs.card_type = 'Attack' OR ufs.split_type = 'Attack') AS cnt
FROM ...
NOTE
DISTINCT is not a function, it's a keyword.
The valid syntax is SELECT DISTINCT expr1, expr2, ... exprN FROM ....
It's invalid to include the DISTINCT keyword multiple times after the SELECT keyword, or in a position other than immediately following SELECT. (The DISTINCT keyword can also be included the COUNT() aggregate function, e.g. SELECT COUNT(DISTINCT expr), but that's entirely different than SELECT DISTINCT.
The parens are entirely ignored. That is, SELECT DISTINCT(foo) is identical to SELECT DISTINCT foo. Including parens is entirely unnecessary, and makes it look like DISTINCT is a function (which it is not.)
You can try below query
SELECT DISTINCT ufs.card_type, ufs.split_type, COUNT(*) AS COUNT
FROM jg1_products p
LEFT JOIN jg1_product_types pt ON p.products_type = pt.type_id
LEFT JOIN jg1_products_to_categories ptc ON p.products_id = ptc.products_id
LEFT JOIN jg1_cards_ufs ufs ON ufs.products_id = p.products_id
WHERE type_handler LIKE "%product_cards%"
AND ptc.categories_id = 89
GROUP BY ufs.card_type
here query will select unique records from card_type and split_type
For more Link can help you.
try this:
SELECT COUNT(DISTINCT(ufs.rarity)) AS COUNT ...
Enjoy your code!